Putting JSON into an Array

前端 未结 3 1978
花落未央
花落未央 2020-12-12 05:59

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

相关标签:
3条回答
  • 2020-12-12 06:00

    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"
      }
    ]
    
    0 讨论(0)
  • 2020-12-12 06:09

    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)

    0 讨论(0)
  • 2020-12-12 06:12

    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

    0 讨论(0)
提交回复
热议问题