[英]Parsing json data not showing correct value
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.krsconnect.no/community/api.html?method=bareListEventsByCategory&appid=620&category-selected=350&counties-selected=Vest-Agder,Aust-Agder"]];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSDictionary *object = [parser objectWithString:json_string error:nil];
NSArray *results = [parser objectWithString:json_string error:nil];
NSDictionary *dictOne = [results objectAtIndex:0];
appDelegate.books = [[NSMutableArray alloc] init];
NSArray *activitiesArray = [dictOne objectForKey:@"events"];
NSDictionary *dictTwo = [activitiesArray objectAtIndex:0];
NSArray *eventArray=[dictTwo objectForKey:@"event"];
NSDictionary *dictThree=[eventArray objectAtIndex:0];
// NSLog(@"%@ - %@", [dictOne objectForKey:@"date"]);
// NSLog(@"%@ - %@", [dictTwo objectForKey:@"affectedDate"]);
NSString*date=[dictOne objectForKey:@"date"];
NSLog(@"%@ - %@", [dictTwo objectForKey:@"affectedDate"]);
NSLog(@"%@ - %@", [dictThree objectForKey:@"title"]);
NSLog(@"%@ - %@", [dictThree objectForKey:@"location"]);
NSLog(@"%@ - %@", [dictThree objectForKey:@"municipality"]);
when i NSLog(title) location and municipality it shows nil values 当我NSLog(title)位置和市镇时,它显示nil值
JSON: JSON:
[
{
"date": 1311552000000,
"events": [
{
"affectedDate": 1311552000000,
"event": {
"appId": 42,
"eventId": 18095,
"location": "Sjølingstad Uldvarefabrik",
"municipality": "Lindesnes",
"title": "Utstillingsåpning - romfisk"
}
},
{
"affectedDate": 1311552000000,
"event": {
"appId": 42,
"eventId": 18095,
"location": "Sjølingstad Uldvarefabrik",
"municipality": "Lindesnes",
"title": "Utstillingsåpning - romfisk"
}
},
{
"affectedDate": 1311580800000,
"event": {
"appId": 620,
"eventId": 19490,
"location": "Høvåg Gjestehus, Vestre Vallesverd, Høvåg",
"municipality": "Lillesand",
"title": "Kunstutstilling på Høvåg Gjestehus"
}
},
...
]
Following Praveen answer I have tried this code but it's still not working: 在Praveen回答之后,我尝试了以下代码,但仍无法正常工作:
NSDictionary *dictOne = [results objectAtIndex:0];
NSArray *activitiesArray = [dictOne objectForKey:@"events"];
NSDictionary *dictTwo = [activitiesArray objectAtIndex:1];
NSArray *eventArray=[dictTwo objectForKey:@"event"];
NSDictionary *dictThree=[eventArray objectAtIndex:2];
JSONValue
that returns NSDictionary
representation of a NSString
. JSONValue
附加解析方法,该方法返回NSString
NSDictionary
表示形式。 After you get a NSDictionary, convert it to NSArray like this NSArray * values = [dictionary allValues];
获得NSDictionary之后,将其转换为NSArray,如下所示:
NSArray * values = [dictionary allValues];
then use method [values objectForKey:@"date"]
to get a date and so on. 然后使用方法
[values objectForKey:@"date"]
获取日期,依此类推。
Its nested Json. 它嵌套的Json。 The parsing and retrieval of values are different.
值的解析和检索是不同的。 Once you get the the dictionary value for a nested object you need to dig into it for getting the next values.
一旦获得了嵌套对象的字典值,就需要深入研究它以获得下一个值。
So basically your code should be 所以基本上你的代码应该是
NSDictionary *dictTwo = [activitiesArray objectAtIndex:1]; // Or get object for key events
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.