In iOS, how do I parse a JSON string into an object

前端 未结 7 838
日久生厌
日久生厌 2021-02-08 05:41

I come from Android dev, so sorry if I\'m missing obvious iOS concepts here.

I have a JSON feed that looks like:

{\"directory\":[{\"id\":0,\"fName\":\"...\

相关标签:
7条回答
  • 2021-02-08 06:12

    Use following code:

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

    This will covert json data onto NSDictionary, which is similar to hashmap on android. I think this will help you. :)

    0 讨论(0)
  • 2021-02-08 06:16

    Use: NSJSONSerialization

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

    An object that may be converted to JSON must have the following properties:
    
    The top level object is an NSArray or NSDictionary.
    All objects are instances of NSString, NSNumber, NSArray, NSDictionary, or NSNull.
    All dictionary keys are instances of NSString.
    Numbers are not NaN or infinity.
    

    You will get NSDictionary then you can parse (create) it to your object and then use it in CoreData.

    0 讨论(0)
  • 2021-02-08 06:21

    I would take a look at RestKit. It provides object-mapping and CoreData backed storage.

    0 讨论(0)
  • 2021-02-08 06:22

    you can use NSJSonSerialisation or AFNetworking library. Here is the example of AFNetworking to parse json response

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:@"http://example.com/resources.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"JSON: %@", responseObject);
        NSDictionary *json = (NSDictionary *)responseObject;
        NSArray *staffArray = json[@"directory"];
    
    [staffArray enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop){
          Staff *staff = [[Staff alloc] init];
        staff.id = [[obj objectForKey:@"id"] integerValue];
        staff.fname = [obj objectForKey:@"fName"];
        staff.lname = [obj objectForKey:@"lName"]; 
    
       //add data to new array to store details
       [detailsArray addObect:staff);
    } ];
    
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }]; 
    

    then use Core Data framework to store data.

    0 讨论(0)
  • 2021-02-08 06:32

    For this, you can SBJSON framework.

    You have to convert the response string into an NSDictionary like

     NSString *responseString = [[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];
        NSDictionary *dic = [responseString JSONValue];
    

    Now you can create an object for Staff class.

    Staff *staff = [[Staff alloc]init];
    

    Then you can store values in this object like

    staff.firstname = [[[dic objectForKey:@"directory"] objectAtIndex:0] objectForKey:@"fName"];
    

    Now you can pass this single object to other classes

    0 讨论(0)
  • 2021-02-08 06:33

    You can use the handmade solution proposed by @janak-nirmal, or use a library like jastor, https://github.com/elado/jastor, it doesn't make much difference. I warn you against Restkit, because the ratio benefits-vs-pain is very low, in my opinion. Moreover, it could be as use a tank to kill a fly in your scenario.

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