如何解析JSON对象数组的NSString?

时间:2015-02-09 06:12:09

标签: ios objective-c json nsstring

我无法将JSON对象的NSString解析为不同的字段。从表中提取JSON对象。检索JSON对象的代码如下所示:

NSString* retrievedStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

             NSCharacterSet *delimiters = [NSCharacterSet characterSetWithCharactersInString:@"{}"];
             NSArray *splitString = [retrievedStr componentsSeparatedByCharactersInSet:delimiters];

splitString的输出:

"Array[",

  "\"image_link\":\"schedule_miss_vn\",\"start_time\":\"17:00\",\"end_time\":\"17:30\",\"viet_performer\":\"\\u0000N\\u0000h\\u0000?\\u0000m\\u0000 \\u0000L\\u0000e\\u0000g\\u0000e\\u0000n\\u0000d\\u0000a\\u0000r\\u0000y\",\"english_performer\":\"Legendary Group\",\"viet_event\":null,\"english_event\":\"Lion Dance\",\"day\":0,\"stage\":0",

    ",",

  "\"image_link\":\"schedule_miss_vn\",\"start_time\":\"17:30\",\"end_time\":\"18:00\",\"viet_performer\":\"\\u0000I\\u0000v\\u0000a\\u0000n\\u0000 \\u0000C\\u0000h\\u0000e\\u0000o\\u0000n\\u0000g\",\"english_performer\":\"Ivan Cheong\",\"viet_event\":\"Ca Nh?c\",\"english_event\":\"Singing\",\"day\":0,\"stage\":0",

    ",",

.....

它基本上是一个JSON对象数组。

我想要检索并存储NSStrings中每个JSON对象的每个字段的值(即start_time,end_time等的值),以便我可以在UITableView中填充它,但我不知道如何解析检索到的NSString以实现我想要的。

任何人都可以提供一些帮助吗?

由于

更新: retrieveStr输出如下所示:

Array[{"image_link":"schedule_miss_vn","start_time":"17:00","end_time":"17:30","viet_performer":"\u0000N\u0000h\u0000?\u0000m\u0000 \u0000L\u0000e\u0000g\u0000e\u0000n\u0000d\u0000a\u0000r\u0000y","english_performer":"Legendary Group","viet_event":null,"english_event":"Lion Dance","day":0,"stage":0},{"image_link":"schedule_miss_vn","start_time":"17:30","end_time":"18:00","viet_performer":"\u0000I\u0000v\u0000a\u0000n\u0000 \u0000C\u0000h\u0000e\u0000o\u0000n\u0000g","english_performer":"Ivan Cheong","viet_event":"Ca Nh?c","english_event":"Singing","day":0,"stage":0},
....
]

7 个答案:

答案 0 :(得分:1)

在头部插入{,在尾部插入}。并尝试

NSError *error = nil;
id appData = [NSJSONSerialization JSONObjectWithData:respose options:NSJSONReadingAllowFragments error:&error];

编辑: 您的JSON不合适。 应该是这样的:

    { "Array" : [{"image_link":"schedule_miss_vn","start_time":"17:00","end_time":"17:30","viet_performer":"\u0000N\u0000h\u0000?\u0000m\u0000 \u0000L\u0000e\u0000g\u0000e\u0000n\u0000d\u0000a\u0000r\u0000y","english_performer":"Legendary Group","viet_event":null,"english_event":"Lion Dance","day":0,"stage":0},
{"image_link":"schedule_miss_vn","start_time":"17:30","end_time":"18:00","viet_performer":"\u0000I\u0000v\u0000a\u0000n\u0000 \u0000C\u0000h\u0000e\u0000o\u0000n\u0000g","english_performer":"Ivan Cheong","viet_event":"Ca Nh?c","english_event":"Singing","day":0,"stage":0}

    ]}

答案 1 :(得分:0)

查看NSJsonSerialization班级

答案 2 :(得分:0)

有明确定义的开源库,可以为您完成这项工作。

我建议的是:JSONModel https://github.com/icanzilb/JSONModel

答案 3 :(得分:0)

您是否尝试过JSON加速器

https://itunes.apple.com/in/app/json-accelerator/id511324989?mt=12

它会将您的JSON转换为Object

答案 4 :(得分:0)

我不明白为什么你的数据保持这种状态,但是:
要获得有效的JSON,您需要删除"数组"来自你的String。 然后,您可以使用NSJSONSerialization

所以:

retrievedStr = [retrievedStr substringWithRange:NSMakeRange(0, [@"Array" length])];
NSError *error;
NSArray *yourJSONArray = [NSJSONSerialization JSONObjectWithData:[str dataUsingEncoding:NSUTF8StringEncoding] options:0 error:&error];

答案 5 :(得分:0)

有效的JSON结构不会包含“Array”一词。不确定如何获取数据。如果返回的数据已经是Objective-C数组(就像您提供的那样),那么您的数据结构就是JSON对象的数组。您需要循环遍历数组,然后解码JSON对象。

答案 6 :(得分:0)

使用NSJSONSerialization来解析json结果。

NSError *error;
    id result = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];