-2

我对这个 json 数据有疑问:

{"return":"{\"productsList\":
[{\"BrandUID\":0,\"BrandName\":\"KAIREE\",\"ProductUID\":213,\"ProductName\":\"ANARKALI NET 
WITH BANARASI 
INNER\",\"Address\":\"\",\"Email\":\"\",\"Phone\":\"\",\"Price\":\"15000.00\",\"MainImage\":
\"http:\\\/\\\/tagin.xspl.in:8888\\\/images\\\/medium\\\/IMG_2635.JPG\",\"TagCount\":0},
{\"BrandUID\":0,\"BrandName\":\"KAIREE\",\"ProductUID\":245,\"ProductName\":\"SILK CHOODIDAR
 SUIT\",\"Address\":\"\",\"Email\":\"\",\"Phone\":\"\",\"Price\":\"5195.00\",\"MainImage\":\
"http:\\\/\\\/tagin.xspl.in:8888\\\/images\\\/medium\\\/IMG_2696.JPG\",\"TagCount\":0},
{\"BrandUID\":0,\"BrandName\":\"ROOP SANGAM\",\"ProductUID\":124,\"ProductName\":\"KOTA 
SILK\",\"Address\":\"\",\"Email\":\"\",\"Phone\":\"\",\"Price\":\"1050.00\",\"MainImage\":\"
http:\\\/\\\/tagin.xspl.in:8888\\\/images\\\/medium\\\/DSC_0181.JPG\",\"TagCount\":0}]}"}

可以请一些帮助我...我想要图像 url 数组,即键MainImage

我该怎么做NSJSONSerialization

4

4 回答 4

2

尝试这个

 id mainDict = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:&parseError];

注意:-这里的数据是您来自服务器的响应。

现在,当您仔细查看上面的JSON数据时,包裹在{(大括号)中的内容是字典的形式。而包裹在[ (方括号)里面的东西都是数组的形式。

所以你可以通过下面的代码来解析你的数据,因为我们的数据在 maindict

NSArray *array = [[maindict valueForKey:@"return"]valueForKey:@"productsList"]]; 

现在,当您打印数组时,您将在数组中获得 3 个元素。

于 2013-09-11T11:51:02.637 回答
2

这看起来像“嵌套 JSON”:“return”键的值是一个字符串,它本身包含一个完整的 JSON 对象。在这种情况下,您必须反序列化两次,如下所示:

NSData *jsonData = ... // your JSON data
NSError *error;

NSDictionary *outerObject = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error: &error];

NSData *innerJson = [outerObject[@"return"] dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *innerObject = [NSJSONSerialization JSONObjectWithData:innerJson options:0 error:&error];

现在您可以访问内部 JSON 对象,例如:

NSArray *productsList = innerObject[@"productsList"];
NSArray *mainImages = [productsList valueForKey:@"MainImage"];
于 2013-09-11T12:00:45.160 回答
1

您确定您的 JSON 数据有效吗?例如,这是有效的 JSON

{"\"key\"": "value"} 

这不是:

{\"key\": "value"}
于 2013-09-11T11:46:21.757 回答
0

请检查 JSON 数据,我已修改,带有额外的“}”

{
"productsList": [
    {
        "BrandUID": 0,
        "BrandName": "KAIREE",
        "ProductUID": 213,
        "ProductName": "ANARKALINETWITHBANARASIINNER",
        "Address": "",
        "Email": "",
        "Phone": "",
        "Price": "15000.00",
        "MainImage": "http: //tagin.xspl.in: 8888/images/medium/IMG_2635.JPG",
        "TagCount": 0
    },
    {
        "BrandUID": 0,
        "BrandName": "KAIREE",
        "ProductUID": 245,
        "ProductName": "SILKCHOODIDARSUIT",
        "Address": "",
        "Email": "",
        "Phone": "",
        "Price": "5195.00",
        "MainImage": "http: //tagin.xspl.in: 8888/images/medium/IMG_2696.JPG",
        "TagCount": 0
    },
    {
        "BrandUID": 0,
        "BrandName": "ROOPSANGAM",
        "ProductUID": 124,
        "ProductName": "KOTASILK",
        "Address": "",
        "Email": "",
        "Phone": "",
        "Price": "1050.00",
        "MainImage": "http: //tagin.xspl.in: 8888/images/medium/DSC_0181.JPG",
        "TagCout": 0
    }
]

}

于 2013-09-11T12:23:23.483 回答