繁体   English   中英

如何快速从JSON获取特定值

[英]How to get specific value from JSON in swift

我可以成功解析json文件中的所有数据。 在集合视图中的应用程序中,我尝试仅将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)")
                }


            }
        }

这是屏幕截图。 您可以看到椅子的不同颜色,我希望每个项目只有一种颜色

在此处输入图片说明

试试这个:

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)")
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM