0

This may be a very simple answer but I cant find a good tutorial to help me. I am trying to parse a json string that I had returned from a http post the json string looks like this when I log it:

(
        {
        "ADD_CITY" = BEDFORD;
        "ADD_LINE1" = "100 COURT AVE";
        "ADD_LINE2" = "";
        "ADD_STATE" = IA;
        "ADD_ZIP" = 50833;
        "BUSINESS_NAME" = "";
        "FIRST_NAME" = JOHN;
        "LAST_NAME" = DOE;
    },
        {
        "ADD_CITY" = BEDFORD;
        "ADD_LINE1" = "101 COURT AVE";
        "ADD_LINE2" = "";
        "ADD_STATE" = IA;
        "ADD_ZIP" = 50833;
        "BUSINESS_NAME" = "";
        "FIRST_NAME" = BOB;
        "LAST_NAME" = JOHNSON;
    }
)

I am trying to make so I can insert each value of each place into a coredata table that I have made but I am getting stuck parsing this into something I can work with. I have read I have to do something with using NSJSONSerialization.

edit

-(void)getLocations{
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"code": @"123"};
[manager POST:@"website.com/getjson" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);
    response = responseObject;
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];
}
BluGeni
  • 3,378
  • 8
  • 36
  • 64
  • That's not JSON at all. If it was just changing `(...)` to `[...]`, it would be easy to fix, but it has a number of other deviations: using `=` instead of `:`, using `;` instead of `,`, and using unquoted strings. You'll have to write your own parser. – ikegami Dec 19 '13 at 17:48
  • 2
    This is the `description` of the object you got from your JSON after deserialization (an `NSArray` in this case containing two `NSDictionaries`). Just enumerate it and you'll be good to go... – Alladinian Dec 19 '13 at 17:51
  • That's an NSArray of two NSDictionaries. What problem are you having with it? – Hot Licks Dec 19 '13 at 17:51
  • @ikegami I added the code that I am using to get my log, hope that helps make sense of things. – BluGeni Dec 19 '13 at 17:54
  • @Alladinian can you give me a brief example of how this would look so I can get off to a start. – BluGeni Dec 19 '13 at 17:57

2 Answers2

0

Yes. NSJsonSerialization does the job. It converts the json to a NSDictionary.

This gives you a fine example.

Community
  • 1
  • 1
Srikanth R
  • 410
  • 5
  • 17
  • 1
    Well, in this case it converted the JSON into an NSArray (of NSDictionaries). What you get is what's in the JSON -- it's not always the same. – Hot Licks Dec 19 '13 at 17:52
  • So in this case how would I say, print/log the first name of the 1st result? – BluGeni Dec 19 '13 at 17:55
  • You have got an array of dictionaries. NSDictionary *firstEntry = [parsedjson objectAtIndex:1]; NSLog(@"%@", [firstEntry objectForKey:@"FIRST_NAME"]; – Srikanth R Dec 19 '13 at 17:57
0

AFNetworking already done this for you (the deserialization from the JSON). So you could just enumerate and create your objects accordingly for storing in CoreData.

-(void)getLocations{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSDictionary *parameters = @{@"code": @"123"};
    [manager POST:@"website.com/getjson" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"JSON: %@", responseObject);
        response = responseObject;

        for (NSDictionary *obj in responseObject) {
            // Create any objects for Coredata here
        }

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
}
Alladinian
  • 34,483
  • 6
  • 89
  • 91
  • hmm I did not realize that this was done for me already. Awesome I think I can get it from here! Thank you for explaining that to me – BluGeni Dec 19 '13 at 18:02