0

Can anyone tell me how to parse my json data in IOS5. I'm providing my JSON data below:

{
 "fieldType" : "Alphanumeric",
 "fieldName" : "Name"
},{
 "fieldType" : "Numeric",
 "fieldName" : "Card Num"
},{
 "fieldType" : "Alphanumeric",
 "fieldName" : "Pin Num"
}

Also is this JSON format correct or do I need to change the JSON format? When I try to parse JSON using below code I get an error:

The operation couldn’t be completed. (Cocoa error 3840.)

The code I'm using:

NSError *error = nil;
NSData *jsonData = [filedList dataUsingEncoding:[NSString defaultCStringEncoding]];
if (jsonData) 
{
    id jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];

    if (error)
    {
        NSLog(@"error is %@", [error localizedDescription]);
        // Handle Error and return
        return;

    }
    NSArray *keys = [jsonObjects allKeys];

    // values in foreach loop
    for (NSString *key in keys)
    {
        NSLog(@"%@ is %@",key, [jsonObjects objectForKey:key]);
    }                
} 
else 
{
    // Handle Error 
}
1
  • use jsonlint.com to validate your JSON. It's invalid Commented Sep 23, 2012 at 10:11

2 Answers 2

3

The JSON data is not correctly formatted. Since you have an array of items, you need to enclose this in [ ... ]:

[
    {
     "fieldType" : "Alphanumeric",
     "fieldName" : "Name"
    },{
     "fieldType" : "Numeric",
     "fieldName" : "Card Num"
    },{
     "fieldType" : "Alphanumeric",
     "fieldName" : "Pin Num"
    }
]

Now JSONObjectWithData gives you an NSMutableArray of NSMutableDictionary objects (because of the NSJSONReadingMutableContainers flag).

You can walk through the parsed data with

for (NSMutableDictionary *dict in jsonObjects) {
    for (NSString *key in dict) {
        NSLog(@"%@ is %@",key, [dict objectForKey:key]);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

In any type of parsing, first of all NSLog the JSON or XML string then start writing your parsing your code.

In your case as per the JSON string you mentioned its a array of dictionaries, and once you got your jsonObjects do this to get your data..

 id jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
NSLog(@"%@",jsonObjects);
// as per your example its an array of dictionaries so

NSArray* array = (NSArray*) jsonObjects;
for(NSDictionary* dict in array)
{
NSString* obj1 = [dict objectForKey:@"fieldType"];
NSString* obj2 = [dict objectForKey:@"fieldName"];

enter code here
enter code here
}

In this way you can parse the your json string.. for more details go through this tutorial by Raywenderlich.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.