1

I Want to parse complex JSON structure JSON is given Below USING IOS.

What lib should i use, or any other lib`s custom available

{
     "firstName": "John",
     "lastName": "Smith",
     "age": 25,
     "address":
     {
         "streetAddress": "21 2nd Street",
         "city": "New York",
         "state": "NY",
         "postalCode": "10021"
     },
     "phoneNumber":
     [
         {
           "type": "home",
           "number": "212 555-1234"
         },
         {
           "type": "fax",
           "number": "646 555-4567"
         }
     ]
 }
3
  • This is what I use in some of my objc projects. github.com/EasyMapping/EasyMapping Commented Dec 5, 2015 at 6:17
  • thanks i will try this . Commented Dec 5, 2015 at 6:29
  • if any1 have code for this this plz share ? .. code i used not working .... Commented Dec 6, 2015 at 7:20

1 Answer 1

1

Read the JSON data into an NSData object, and use NSJSONSerialization's JSONObjectWithData:options:error: method. The result will be an NSDictionary containing NSString, NSDictionary, and NSArray objects.

So for example:

NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"file:///myfile.txt"]]; // Probably get this from somewhere else, but you get the idea.

NSError *error = nil;
id topObject = [NSJSONSerialization JSONObjectWithData:data options:0 error: &error];

if ([topObject isKindOfClass:[NSDictionary class]] && !error) {
    ...
}
Sign up to request clarification or add additional context in comments.

2 Comments

Not sure how I could give a useful example of that. It's a single method that takes an NSData object containing the JSON data, pass 0 for options, and pass a reference to an NSError *error = nil; to get the error back.
Okay, minimal example, but the only really interesting bit is going to be getting the data into the NSData object to begin with, which is entirely dependent on what you're doing. :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.