json - 如何在 swift 中从 JSON 获取特定值

标签 json swift parsing repeat

我可以成功解析 json 文件中的所有数据。在我的应用程序中的 Collection View 中,我尝试仅将 1 个图像数据解析到单元格中,但我将所有数据与其相关联。我将与您分享 JSON 代码和解析代码,最后分享模拟器的屏幕截图。我希望你能帮助我。 谢谢,

JSON 代码

{
    "retcode": "200",
    "content": [{
        "id": 3,
        "name": "X Treme",
        "desc": "Polikarbon G\u00f6vde",
        "category": "Design",
        "thumbnail": [{
            "id": 2,
            "thumbnail": "http:\/\/www.ertonga.com\/product_images\/xtreme_red.jpg"
        }, {
            "id": 3,
            "thumbnail": "http:\/\/www.ertonga.com\/product_images\/xtreme_orange.jpg"
        }, {
            "id": 4,
            "thumbnail": "http:\/\/www.ertonga.com\/product_images\/xtreme_blue.jpg"
        }, {
            "id": 5,
            "thumbnail": "http:\/\/www.ertonga.com\/product_images\/xtreme_green.jpg"
        }, {
            "id": 6,
            "thumbnail": "http:\/\/www.ertonga.com\/product_images\/xtreme_clear.jpg"
        }, {
            "id": 7,
            "thumbnail": "http:\/\/www.ertonga.com\/product_images\/xtreme_grey.jpg"
        }, {
            "id": 8,
            "thumbnail": "http:\/\/www.ertonga.com\/product_images\/xtreme_slred.jpg"
        }]
    }, {
        "id": 4,
        "name": "Opal",
        "desc": "Polikarbon Sandalye",
        "category": "Design",
        "thumbnail": [{
            "id": 9,
            "thumbnail": "http:\/\/www.ertonga.com\/product_images\/opal_orange.jpg"
        }, {
            "id": 10,
            "thumbnail": "http:\/\/www.ertonga.com\/product_images\/opal_blue.jpg"
        }, {
            "id": 11,
            "thumbnail": "http:\/\/www.ertonga.com\/product_images\/opal_green.jpg"
        }]
    }],
    "error_msg": ""
}

Swift 代码

if let url = NSURL(string: urlString) {

            if let data = try? NSData(contentsOfURL: url, options: [])

            {


                do {
                    let json = try NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments)


                    var valueforname:Int = 0

                    if let blogs = json["content"] as? [[String: AnyObject]] {


                        for blog in blogs {

                            if let blog2 = blog["thumbnail"] as? [[String: AnyObject]] {

                                for blog3 in blog2 {

                                    if let blog4 = blog3["thumbnail"] as? String {

                                        var checkvalue1 = blog3["id"] as? Int





                                        if Imagearray.contains(String(checkvalue1!)) {


                                                                    }

                                        else {


                                                Imagearray.append(blog4)
                                            }
                                        }





                                    }

                                }




                        }
                    }
                }





                catch {
                    print("error serializing JSON: \(error)")
                }


            }
        }

这是屏幕截图。您可以看到椅子的不同颜色,我只想每件商品只有一种颜色

enter image description here

最佳答案

试试这个:

NSURL(string: urlString) {

    if let data = try? NSData(contentsOfURL: url, options: [])

    {
        do {
            let json = try NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments)
            var valueforname:Int = 0
            if let blogs = json["content"] as? [[String: AnyObject]] {
                for blog in blogs {
                    if let blog2 = blog["thumbnail"] as? [[String: AnyObject]] {
                        if let blog4 = blog2["thumbnail"] as? String
                        {
                            Imagearray.append(blog4)
                        }
                    }
                }
            }
        }
        catch {
            print("error serializing JSON: \(error)")
        }
    }
}

关于json - 如何在 swift 中从 JSON 获取特定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36331055/

相关文章:

Java飞行记录器以编程方式解析

parsing - 转换 Attoparsec 解析器以解析其他字符串类型

python - 如何遍历嵌套的字典列表?

java - Spring REST,JSON "Can not handle managed/back reference ' defaultReference'"415 不支持的媒体类型

java - 将字符串数组的 json 对象映射到 java 类

swift - 可编码为 CKRecord

swift - 图像纵横比 swift ios

swift - 无法将类型 'String' 的值转换为预期的参数类型

c# - 我如何在 C# 中解析文本文件

java - 谷歌分析异常