如何解析iPhone中的Json值?

时间:2013-01-31 09:13:19

标签: iphone ios json parsing

  

可能重复:
  How to parsing JSON object in iPhone SDK (XCode) using JSON-Framework

我在iPhone应用程序中工作,使用JSON框架从Web服务解析Json值, 如何得到像1221,1278,3456,...........等的Json值(这个值不是常量,它是自动动态改变值,所以我不知道) 有可能吗?

先谢谢

JSON响应供您参考:

  {
        "1221": 
    {
            "type": "product",
            "product_id": 1221,
            "intID": "1",
            "name": "rer Margherita",
            "des": "Tomatensauce, Käse<sup>1</sup>",
            "img": "",
            "isDeal": false,
            "cat": {
                "1": {
                    "price": 4,
                    "pos": 1,
                    "catname": "normal",
                    "extras": false
                },
                "2": {
                    "price": 5.9,
                    "pos": 2,
                    "catname": "groß",
                    "extras": false
                }
            }
        },
        "1278": {
            "type": "product",
            "product_id": 1222,
            "intID": "2",
            "name": "ere Zwirerebeln",
            "des": "er",
            "img": "",
            "isDeal": false,
            "cat": {
                "1": {
                    "price": 2,
                    "pos": 1,
                    "catname": "rer",
                    "extras": true
                },
                "2": {
                    "price": 6.2,
                    "pos": 2,
                    "catname": "mega",
                    "extras": true
                }
            }
        },

1 个答案:

答案 0 :(得分:2)

只需将您的JSON数据转换为对象

NSData *jsonData = //response data

NSDictionary *object = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];

然后没有涉及解析,你只需要处理字典。 (或阵列等......)

要获得您想要的列表,您可以执行...

NSArray *requiredValues = [object allKeys];