Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
223 views
in Technique[技术] by (71.8m points)

ios - Putting JSON into an Array

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.

My response looks like this.

{
    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";
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...