2011-06-01 72 views
-1

这是我的JSON数据:如何分割JSON响应分为三个不同的阵列

{ 
    "SearchList": [ 
     { 
      "HeadLines NewsIds ": [ 
       "1056305", 
       "1285209", 
       "3248204" 
      ], 
      "NewsText NewsIds ": [ 
       "12328475", 
       "12328324", 
       "12328310", 
       "12325544" 
      ], 
      "Issuers News Ids": [ 
       "4269", 
       "21636", 
       "21828", 
       "22061" 
      ] 
     } 
    ] 
} 

从这个杰森甲酸我需要存储 头条新的ID 的新闻稿newsids 发行新闻IDS

分为三不同阵列.....

+1

请在这里明确你想要做什么,并制定一个可以理解的问题。另外,这不是JSON格式,它只是一堆数据。 – marzapower 2011-06-01 08:20:06

+0

@Bavarious @marzapower这绝对是有效的json,你们有没有试图在firebug中运行它?我不明白OP要做什么。 – 2011-06-01 08:30:49

+0

http://jsonlint.com/表示它是有效的JSON。 – ThomasW 2011-06-01 08:33:32

回答

1

安装JSONKit并使用此代码:

+(NSDictionary*) parseJson:(NSString*) jsonString {  
    NSDictionary *rootDict = nil; 
    NSError *error = nil; 
    @try { 
     JKParseOptionFlags options = JKParseOptionComments | JKParseOptionUnicodeNewlines; 
     rootDict = [jsonString objectFromJSONStringWithParseOptions:options error:&error]; 
     if (!rootDict) { 
      NSLog(@"%@",[error localizedDescription]); 
     } 
    } @catch (NSException * e) { 
     NSLog(@"%@ %@", [e name], [e reason]); 
     rootDict = nil; 
    } 
    return rootDict; 
} 

然后迭代产生NSDictionary这样的:

NSDictionary *root = [Yourclass parseJson:string]; 
NSDictionary *elements = [[root objectForKey:@"SearchList"] lastObject]; 
NSArray *headlines = [elements objectForKey:@"HeadLines NewsIds"]; 
// ... 
+0

固定, 谢谢。我需要阅读有关错误处理。 – Jano 2011-06-01 10:13:47

0
- (void)interceptUrlRequest1:(NSString*)urlString withString:(NSString*)string { 
    FavouriteData *favData=[[SharedObjects sharedInstance] favouriteData]; 
    NSRange match = [urlString rangeOfString:string]; 
if ([string isEqualToString:@"search.htm"]) { 
    if (match.location!=NSNotFound) { 
     NSString *url=[NSString stringWithString:@"url............"]; 
     NSData *data1=[NSData dataWithContentsOfURL:[NSURL URLWithString:url]]; 
     NSString *response=[[NSString alloc]initWithData:data1 encoding:nil]; 

     SBJsonParser *parser=[SBJsonParser new]; 


     NSDictionary *root = [Webview parser:string]; 
     NSDictionary *elements = [[root objectForKey:@"SearchList"] lastObject]; 
     NSArray *headlines = [elements objectForKey:@"HeadLinesNewsIds"]; 
       NSArray *newstext=[element objectForKey:@"NewsTextNewsIds"]; 
       NSArray *issuersnews=[element objectForKey:@"IssuersNewsIds"]; 

我的班级网页视图。