2010-03-10 156 views
6

我有以下JSON对象:解析使用JSON框架嵌套JSON对象为目标C

{ 
"response": { 
    "status": 200 
}, 
"messages": [ 
    { 
     "message": { 
      "user": "value" 
      "pass": "value", 
      "url": "value" 
     } 
] 
    } 

}

我使用JSON-框架(也尝试JSON触摸)通过该解析和创建一本字典。我想访问“消息”块,并取出“用户”,“传递”和“网址”值。

在对象 - 我有以下代码:

// Create new SBJSON parser object 
SBJSON *parser = [[SBJSON alloc] init]; 

// Prepare URL request to download statuses from Twitter 
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:myURL]]; 

// Perform request and get JSON back as a NSData object 
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 

// Get JSON as a NSString from NSData response 
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]; 

//Print contents of json-string 
NSArray *statuses = [parser objectWithString:json_string error:nil]; 
NSLog(@"Array Contents: %@", [statuses valueForKey:@"messages"]); 
NSLog(@"Array Count: %d", [statuses count]); 


NSDictionary *results = [json_string JSONValue]; 
NSArray *tweets = [[results objectForKey:@"messages"] objectForKey:@"message"]; 

for (NSDictionary *tweet in tweets) 
{ 
    NSString *url = [tweet objectForKey:@"url"]; 
    NSLog(@"url is: %@",url); 
} 

我可以拉出来“消息”和看到所有的“消息”块,但我无法解析更深并拉出“用户“,”通行证“和”网址“。

+0

的JSON字符串格式不正确,就像你在这里写的那样。消息数组有两个开放的花括号,并且只有一个关闭。 – Felixyz 2010-03-10 09:05:11

回答

9

解决:

NSArray *tweets = [[results objectForKey:@"messages"] valueForKey:@"message"]; 
+0

你能多解释一下这个答案吗? – Dewseph 2015-04-24 02:37:25

6
Array({ 

    0=>Dictionary({ 

     response = Array({ 

     0=>Dictionary(Status = 200) 

     }) 

    }), 

    1=>Dictionary({ 

     messages = Array({ 

     0=> Dictionary({ 

      message = Array({ 

      0=>Dictionary({ 

       user = value, 

       pass=value, 

       url=value 

      }) 

      }) 

     }) 

     }) 

    }) 

}) 

因此,访问字典用户,传球,网址,

nsarray *arr = jsonmainarray; 


arr = [[[jsonmainarray objectAtIndex: 1] objectforkey:@"messages"] objectatindex: 0]; 



nsdictionary *dict = [arr objectatindex: 0]; 

arr = [dict objectforkey:@"message"]; 

dict = [arr objectatindex: 0]; // Dictionary with user, pass, url 
+0

Gr8 ................ – 2010-03-31 09:41:45

2

还有一个更简单的方法(在我看来)做JSON解析: )

- (void)loadJSONData:(NSString *)u{ 
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://expandurl.appspot.com/expand?url=%@", u]]; 
    NSData *rawJsonData = [NSData dataWithContentsOfURL:url]; 

    CJSONDeserializer *parser = [CJSONDeserializer new]; 
    NSError *error; 
    NSDictionary *jsonDictionary = [parser deserializeAsDictionary:rawJsonData error:&error]; 

    [parser release]; 

    NSArray *array = [jsonDictionary objectForKey:@"urls"]; 
} 

所有你需要做的就是使用JSON触摸...像希汉阿拉姆提到的。

说,你有这一行的JSON数据:

{ “end_url”= “http://www.youtube.com”; 重定向= 0; “start_url”=“http://www.youtube.com”; status = OK; 网址=( “http://www.youtube.com” ); }

然后在JSONDictonary中的数据可以通过做访问:

[jsonDictionary objectForKey:@"urls"]; //This will return an Array since URLS is a array 
[jsondictionary objectForKey:@"en_url"]; //this will return an NSString since end_url is a string 

希望这有助于人一样,因为它帮助我=)

你真诚 克里斯蒂安