我有以下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);
}
我可以拉出來「消息」和看到所有的「消息」塊,但我無法解析更深並拉出「用戶「,」通行證「和」網址「。
的JSON字符串格式不正確,就像你在這裏寫的那樣。消息數組有兩個開放的花括號,並且只有一個關閉。 – Felixyz 2010-03-10 09:05:11