将JSON提要转换为NSDictionary [英] Convert JSON feed to NSDictionary
本文介绍了将JSON提要转换为NSDictionary的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
其中 JSON_CATEGORY_DATA_URL_STRING
是我的Feed网址,返回正常:
Where JSON_CATEGORY_DATA_URL_STRING
is my feed URL, which returns fine as:
[
{
"group":"For Sale",
"code":"SSSS"
},
{
"group":"For Sale",
"category":"Wanted",
"code":"SWNT"
}
]
我似乎无法获得一个不错的 NSDictionary
(或 NSArray
)以下代码:
I cannot seem to get a nice NSDictionary
(or NSArray
) out of the following code:
+ (NSDictionary *)downloadJSON
{
NSDictionary *json_string;
NSString *dataURL = [NSString stringWithFormat:@"%@", JSON_CATEGORY_DATA_URL_STRING];
NSLog(@"%@",dataURL);
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:dataURL]];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
json_string = [[[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]autorelease];
NSDictionary *json_dict = (NSDictionary *)json_string;
NSLog(@"json_dict\n%@",json_dict);
NSLog(@"json_string\n%@",json_string);
return json_string;
}
我已经阅读了很多这方面的帖子,但我没有得到它。
I've read many posts on this, but am not getting it.
推荐答案
使用IOS5,您可以使用 NSJSONSerialization 。
With IOS5 you can use NSJSONSerialization for serializing the JSON.
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
希望这对你有帮助。
这篇关于将JSON提要转换为NSDictionary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文