Iphone 如何解析这个JSON提要?
我已经发布了这个来寻求帮助,但不幸的是没有一个对我有用。。。 有人来帮我吗。。请…如何解析此JSON提要。我想获得所有项目,如Iphone 如何解析这个JSON提要?,iphone,ios,Iphone,Ios,我已经发布了这个来寻求帮助,但不幸的是没有一个对我有用。。。 有人来帮我吗。。请…如何解析此JSON提要。我想获得所有项目,如'id','title','striser','body','fid','filename','filepath' { "data":{ "mat_149":{ "id":"149", "title":"The closing of 40% profit within 9 month",
'id','title','striser','body','fid','filename','filepath'
{
"data":{
"mat_149":{
"id":"149",
"title":"The closing of 40% profit within 9 month",
"teaser":"profit within 9 months only which is equal to 52% annual profit",
"body":" The auction was presented in a very high and commercial lands.\u000d\u000a",
"files":{
"911":{
"fid":"911",
"filename":"22.JPG",
"filepath":"http://mysite/files/22_0.JPG"
}
}
},
"mat_147":{
"id":"147",
"title":"Company launches the city ",
"teaser":"demands for distinguished lands.",
"body":" The area size is quare meters This is evident through projects and many other projects.\u000d\u000a\u000d\u000a",
"files":{
"906":{
"fid":"906",
"filename":"2D7z.jpg",
"filepath":"http://mysite/dlr/files/2D7Z.jpg"
}
}
},
"mat_link":"mysite.com/"
}
}
我使用json框架对其进行如下解析:
NSString *response = [[NSString alloc] initWithData:receivedData encoding:NSASCIIStringEncoding] ;
SBJSON *parser = [[SBJSON alloc] init];
NSDictionary *data = (NSDictionary *) [parser objectWithString:response error:nil];
NSLog(@"Data : %@", [data valueForKey:@"data"] );
我得到数据:
NSLog(@"Data : %@", [data objectForKey:@"data"] );
我想获取所有项目'id','title','striser','body','fid','filename','filepath'
。。。。。。。。。请帮帮我。。。。。。请
但问题是。有时它会说
File ------------------- = "<null>";
文件-------------------=”;
我必须做的。。帮帮我……您可以使用这个JSON解析器 然后您就可以这样使用它:
NSDictionary *jsonData = [jsonString JSONValue];
//Get the data object
NSArray *data = (NSArray*)[jsonData objectForKey=@"**data**"];
NSDictionary *user = (NSDictionary*)[element objectForKey:[[element allKeys] objectAtIndex:0]];
现在可以从数组对象访问数据
遍历数据对象
for(int i=0;i<[data count];i++){
NSDictionary *element = (NSDictionary *)[elements objectAtIndex:i];
NSDictionary *user = (NSDictionary*)[element objectForKey:[[element allKeys] objectAtIndex:0]];
NSString *ID = [user objectForKey:@"id"];
NSString *title = [user objectForKey:@"title"];
//...
}
这很容易,您会收到一个包含(主要)字典对象的字典。您可以通过对象的表示法看到这一点,例如: 带字符串的字典:
“aKey”:“aValue”
包含键“aKey”的字符串。字典和字典:
“aDict”:{“aKey”:“aValue”}
是一个名为“aDict”的字典,其中包含一个名为“aKey”的键
因此,如果我们记住这一点,就可以毫不费力地解析JSON字典
NSDictionary *data = (NSDictionary *) [parser objectWithString:response error:nil];
NSDictionary *dict = [data valueForKey:@"data"];
NSDictionary *mat = [dict valueForKey:@"mat_147"];
NSString *id = [mat valueForKey:@"id"];
NSString *title = [mat valueForKey:@"title"];
我想
valueForKeyPath:
也可以用来导航树。对于所有mat####对象,您可能希望使用枚举器遍历每个mat##字典。文件在数据的最后一项中为空
“mat_link”:“mysite.com/”
在此项下没有定义文件元素。谢谢,我想在这里存储一些数组的id、标题、摘要、fid和文件路径。我要做的……。@proCoder查看我的答案。您的数据与您的期望不符。您有一些空元素,因此导致空值。您需要增强对元素的处理,以适应这些情况,因为您的代码假设每个元素都有一个files元素,但事实并非如此。谢谢。。。但是如果我有更多的条目,我应该怎么做才能得到每个mat_链接条目,或者如何遍历每个字典。。。。请对于“文件”,我可以使用NSDictionary*文件=[mat valueForKey:@“文件”];当我像这样使用时,我正在获取文件。。。。NSDictionary*mat=[dict valueForKey:@“mat_147”];NSLog(@“数据:%@”,mat);是否有时
文件
字段是空的?:-)那么有什么需要解决的呢?
NSDictionary *data = (NSDictionary *) [parser objectWithString:response error:nil];
NSDictionary *dict = [data valueForKey:@"data"];
NSDictionary *mat = [dict valueForKey:@"mat_147"];
NSString *id = [mat valueForKey:@"id"];
NSString *title = [mat valueForKey:@"title"];