简体   繁体   English

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

[英]How do I parse an NSString of an array of JSON Objects?

I am having trouble parsing an NSString of JSON objects into different fields. 我在将JSON对象的NSString解析到不同字段时遇到麻烦。 The JSON objects is pulled from the table. JSON对象从表中拉出。 The code to retrieve the JSON object looks like this: 检索JSON对象的代码如下所示:

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

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

The output of splitString: 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",

    ",",

.....

It is basically an array of JSON objects. 它基本上是一个JSON对象数组。

What I would like to do retrieve and store the value of each field (ie value of start_time, end_time, etc.) for EACH JSON object in NSStrings so I can populate it in a UITableView, but I don't know how to parse the retrieved NSString to achieve what I want. 我想为NSStrings中的每个JSON对象检索和存储每个字段的值(即start_time,end_time等的值),以便可以在UITableView中填充它,但是我不知道如何解析检索NSString实现我想要的。

Can anyone provide some assistance? 谁能提供帮助?

Thanks 谢谢

UPDATE: The retrievedStr output looks like this: 更新:retriatedStr输出看起来像这样:

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},
....
]

insert a { in the head and } int the tail. {插入{}插入尾部。 And try 试一下

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

Edit: You're JSON is not proper. 编辑:您的JSON不正确。 Should be like this: 应该是这样的:

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

    ]}

看一下NSJsonSerialization

There are well defined opensource libs which can do this job for you. 有定义明确的开源库可以为您完成这项工作。

One that I would recommend is: JSONModel https://github.com/icanzilb/JSONModel 我建议的一个是:JSONModel https://github.com/icanzilb/JSONModel

Have you try JSON Accelerator 您是否尝试过JSON Accelerator

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

It will convert your JSON into Object 它将您的JSON转换为Object

I don't understood why your data was kept that way, but: 我不明白为什么您的数据会这样保存,但是:
To get a valid JSON, you need to remove the "Array" from your String. 要获取有效的JSON,您需要从字符串中删除“数组”。 Then, you can use NSJSONSerialization . 然后,您可以使用NSJSONSerialization

So: 所以:

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

Valid JSON structure will not have the word 'Array'. 有效的JSON结构将没有单词“ Array”。 Not sure how you get the data. 不确定如何获取数据。 If the returned data is already an Objective-C array (like what you present), your data structure is an array of JSON objects. 如果返回的数据已经是一个Objective-C数组(如您所呈现的那样),则您的数据结构是一个JSON对象数组。 You need to loop through the array and then decode the JSON object. 您需要遍历数组,然后解码JSON对象。

use NSJSONSerialization for parse json result. 使用NSJSONSerialization解析json结果。

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

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

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