ios - 将 JSON 提要转换为 NSDictionary

标签 ios objective-c nsarray nsdictionary sbjson

JSON_CATEGORY_DATA_URL_STRING 是我的 Feed URL,返回结果如下:

[
    {
        "group":"For Sale",
        "code":"SSSS"
    },
    {
        "group":"For Sale",
        "category":"Wanted",
        "code":"SWNT"
    }
]

我似乎无法从以下代码中得到一个不错的 NSDictionary(或 NSArray):

+ (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;
}

我已经阅读了很多关于此的帖子,但我不明白。

最佳答案

在 IOS5 中你可以使用 NSJSONSerialization用于序列化 JSON。

NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

关于ios - 将 JSON 提要转换为 NSDictionary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5038371/

相关文章:

cocoa - NSarray 发布

iphone - 你能从文件夹内容构建一个项目数组吗?

ios - iOS 应用程序 + 服务器集成中的 Paypal 链式支付(或自适应)

ios - 创建注释数组以便在 map 上制作不同的标注 View

iOS : Best practices for Fields Validation on User Interface

iOS 7 - 在某些 View 上隐藏状态栏

ios - 使用多个 UICollectionView,出现错误, "... must be initialized with non-nil layout"

ios - 如何检测 Swift 2 中的所有触摸

iphone - 像 safari 一样在键盘上添加一个 .com 按钮

iphone - 阵列清理方法错误