Putting JSON into an Array

我的未来我决定 提交于 2019-11-28 14:40:46

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

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

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

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

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

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

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

JSON:http://www.json.org

Anh Do

You use the NSJSONSerialization class to convert JSON to Foundation objects and convert Foundation objects to JSON.

This class is available in iOS 5.0+. If you're targetting older iOS version, have a look at a third-party JSON framework:

Comparison of JSON Parser for Objective-C (JSON Framework, YAJL, TouchJSON, etc)

If that's what you're getting back it's not JSON I'm afraid. It does look like Javascript in a way but it should be more like

[
  {
     "id" : 38,
     "name" : "St. Martin Hall"
  },
  {
     "id" : 39,
     "name" : "Assumptions Commons"
  }
]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!