将JSON放入数组中 [英] Putting JSON into an Array

查看:140
本文介绍了将JSON放入数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在请求和JSON方面,我是一个菜鸟。在我的应用程序内部,我发送到服务器并获取东西,所以我当然可以使用它。我尝试查找不同的东西,但似乎没有一个看起来像我正在寻找的东西。所以我回到了似乎是JSON格式的东西。我想知道怎么做是把它放到NSMutable数组中。我获得此JSON的方式是使用AFNetworking的AFJSONRequestOperation。

I'm a noob when it comes to requests and JSON. Inside my app I send to the server and get back stuff so I can use it of course. I tried looking up different things but none really seem to be what I'm looking for. So I'm getting back what seems to be formatted JSON. What I want to know how to do is put it into a NSMutable array. The way I get this JSON is by using AFNetworking's AFJSONRequestOperation.

我的回答是这样的。

{
    id = 38;
    name = "St. Martin Hall";
},
    {
    id = 40;
    name = "Assumptions Commons";
},
    {
    id = 41;
    name = "Vickroy Hall";
},
    {
    id = 42;
    name = "St. Ann Hall";
},
    {
    id = 37;
    name = "Duquesne Towers";
}


推荐答案

如果您的JSON格式如{ mainKey:[{},{},...]}

if your JSON format like {"mainKey":[{},{},...]}

 NSError* error;
    NSDictionary* json = [NSJSONSerialization 
        JSONObjectWithData:responseData //1
         options:kNilOptions 
        error:&error];

    NSArray* dataArray = [json objectForKey:@"mainKey"]; //2

否则您的JSON格式如[{},{},...]

else your JSON format like [{},{},...]

NSError* error;
    NSArray* dataArray = [NSJSONSerialization 
        JSONObjectWithData:responseData //1
        options:kNilOptions 
        error:&error];

我认为你的格式是案例2:[]对象数组{}

I think your format is case 2: [] Array of Object {}

教程: http:// www.raywenderlich.com/5492/working-with-json-in-ios-5

JSON: http://www.json.org

这篇关于将JSON放入数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆