2010-03-10 108 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 

希望這有助於人一樣,因爲它幫助我=)

你真誠 克里斯蒂安