2011-07-17 48 views
-3

我已经发布了这个帮助,但不幸的是他们都没有为我工作... 任何人都在那里帮助我..请.....如何解析此JSON饲料。我想所有的项目,如'id','title','teaser','body','fid,'filename','filepath' .........如何解析此JSON供稿?

{ 
    "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','teaser','body','fid,'filename','filepath' .........请帮我......请.....


NSString *response = [[NSString alloc] initWithData:receivedData encoding:NSASCIIStringEncoding] ; 
SBJSON *parser = [[SBJSON alloc] init]; 
NSDictionary *data = (NSDictionary *) [parser objectWithString:response error:nil]; 

NSDictionary *filedict = [data valueForKey:@"data"]; 
for(id key in filedict) 
{ 
    NSDictionary *files = [filedict valueForKey:key]; 
    NSLog(@"File ------------------- = %@",files); 
} 

但问题是。有时候它说

File ------------------- = "<null>"; 

我有什么做的..帮助我....

+1

难道有时候'files'字段是空的吗? :-)那么有什么要解决的? – DarkDust

+5

你的名字是...讽刺! –

+0

文件的项目是有我的JSON ......它不是空.....请.....它不是空.......文件项是有我的JSON。它不为空... – proCoder

回答

3

您可以使用此JSON解析器https://github.com/stig/json-framework/ 然后,你将能够使用它像这样:

NSDictionary *jsonData = [jsonString JSONValue]; 
//Get the data object 
NSArray *data = (NSArray*)[jsonData [email protected]"**data**"]; 

现在您可以访问数组对象中的数据。

迭代通过您的数据对象

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"]; 
    //... 
} 

注意您的对象键在元件阵列前everyobject不同。 mat_157与mat_156,所以你不能获得统一的键值对象。这就是为什么你必须得到每一个元件中的第一关键,像这样:

NSDictionary *user = (NSDictionary*)[element objectForKey:[[element allKeys] objectAtIndex:0]]; 
+0

感谢,在这里,我想存储ID,标题,传情,FID,文件路径一些阵列。我需要做的..... ...... – proCoder

+0

编辑为你答案。 – Cyprian

+0

我使用上述功能,但prblm是,有时很老的“文件”,“文件路径” ......其返回null .... ..Please .... – proCoder

0

这是很容易,您会收到包含(为主)字典对象的字典。您可以通过对象的符号来查看,例如:

带字符串的字典:"aKey":"aValue"包含关键字'aKey'的字符串。
字典和字典:"aDict":{"aKey":"aValue"}是一个名为'aDict'的字典,页面中包含1个名为'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 _ ###字典。

+0

感谢...但是,如果我有更多的项目,我应该怎么办Ø获取每个mat_link项目或如何通过每个字典遍历....请.. ..'文件'我可以使用NSDictionary *文件= [mat valueForKey:@“文件”]; – proCoder

+0

当我使用这样的,我收到文件....的NSDictionary *垫= [字典valueForKey:@“mat_147”]; NSLog(@“Data:%@”,mat); – proCoder

+0

你可以显示字典对象的日志吗? –

1

文件在数据的最后一项中为空。

“mat_link”:“mysite。COM /”

没有依照本规定没有文件的元素。