xcode 使用 JSON Framework for Objective-C 解析嵌套的 JSON 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2414426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Parsing nested JSON objects with JSON Framework for Objective-C
提问by Sheehan Alam
I have the following JSON object:
我有以下 JSON 对象:
{
"response": {
"status": 200
},
"messages": [
{
"message": {
"user": "value"
"pass": "value",
"url": "value"
}
]
}
}
}
I am using JSON-Framework (also tried JSON Touch) to parse through this and create a dictionary. I want to access the "message" block and pull out the "user", "pass" and "url" values.
我正在使用 JSON-Framework(也尝试过 JSON Touch)来解析它并创建一个字典。我想访问“message”块并提取“user”、“pass”和“url”值。
In Obj-C I have the following code:
在 Obj-C 中,我有以下代码:
// 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);
}
I can pull out "messages" and see all of the "message" blocks, but I am unable to parse deeper and pull out the "user", "pass", and "url".
我可以拉出“消息”并查看所有“消息”块,但我无法更深入地解析并拉出“用户”、“通过”和“url”。
回答by Sheehan Alam
Solved:
解决了:
NSArray *tweets = [[results objectForKey:@"messages"] valueForKey:@"message"];
回答by Paresh Thakor
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
})
})
})
})
})
})
So, to access dictionary for user, pass, url,
因此,要访问用户、pass、url 的字典,
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
回答by Kristian Flatheim Jensen
there is an easier way (in my opinion) to do JSON parsing :)
有一种更简单的方法(在我看来)进行 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"];
}
All you have to do is to use JSON touch... like Sheehan Alam mention.
你所要做的就是使用 JSON touch ......就像 Sheehan Alam 提到的那样。
Say that you got this line of JSON data:
假设你得到了这行 JSON 数据:
{ "end_url" = "http://www.youtube.com"; redirects = 0; "start_url" = "http://www.youtube.com"; status = OK; urls = ( "http://www.youtube.com" ); }
{ "end_url" = "http://www.youtube.com"; 重定向 = 0; "start_url" = "http://www.youtube.com"; 状态 = 正常;urls = ( "http://www.youtube.com" ); }
then in your JSONDictonary the data can be accessed by doing:
然后在您的 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
Hope this help people as much as it helped me =)
希望这对人们的帮助和对我的帮助一样多 =)
sincerely yours Kristian
真诚地你的克里斯蒂安