4

I'm trying to parse a JSON string from a web service. The string that is coming in looks like this:

{
"Faculty_Members": [
    {
        "ID": 3377,
        "First_Name": "John",
        "Last_Name": "Doe"
    }
]
}

My IOS Code looks like this:

NSURL *jsonUrl = [NSURL URLWithString:@"http://website/Service1.svc/Names"];
NSError *error = nil;
NSData *jsonData = [NSData dataWithContentsOfURL:jsonUrl options:kNilOptions error:&error];
NSMutableDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:&error];
NSLog(@"%@",jsonResponse);

//parse out the json data
if([NSJSONSerialization isValidJSONObject:jsonResponse])
{
NSLog(@"YEP");
  }else{
NSLog(@"NOPE");
}

The log will show the correct JSON data, but I keep getting "NOPE" on the isValidJsonObject.

The web service is sending the data back as datatype "string". Does that make a difference? If so, what datatype should i send it back?

Any ideas will be greatly appreciated!

1
  • i am try to parese same type of dic. and getting problem. @ NeedSomeAnswers if you get any solution and post it. Commented Nov 25, 2013 at 13:12

3 Answers 3

3

You don't use isValidJSONObject: to test for a valid JSON string, you use it to test for an object that can be converted to JSON; See the documentation:

isValidJSONObject:

Returns a Boolean value that indicates whether a given object can be converted to JSON data.

+ (BOOL)isValidJSONObject:(id)obj

Parameters:

obj
The object to test.


Return Value:

YES if obj can be converted to JSON data, otherwise NO.

Instead, just use JSONObjectWithData: to parse the data as usual; if it fails, it will return an NSError in error.

Sign up to request clarification or add additional context in comments.

2 Comments

So, does that mean that an NSDictionary is not a valid object to parse with?
@NeedSomeAnswers: I think it is; just make sure you pass the right options.
0

It is also possible that the web service delivers the JSON strings in a wrong coding.

According to the NSJSONSerialization Class Reference!

The data must be in one of the 5 supported encodings listed in the JSON specification: UTF-8, UTF-16LE, UTF-16BE, UTF-32LE, UTF-32BE. The data may or may not have a BOM. The most efficient encoding to use for parsing is UTF-8, so if you have a choice in encoding the data passed to this method, use UTF-8.

2 Comments

Thanks motou. I tried the following code - NSLog(@"%@", [jsonResponse objectForKey:@"First_Name"]); but I get a message that says "unrecognized selector sent to instance". And when i look at the keys of my dictionary, it says that I have none.
Can I encode is on the IOS side, or do i have to do it on the web service side?
0

You should read up on JSON. { } indicates a dictionary. [ ] indicates an array

So your returned JSON object is a dictionary containing an array containing a dictionary. To get the content, you could try the following:

// YOUR CODE
NSURL *jsonUrl = [NSURL URLWithString:@"http://website/Service1.svc/Names"];
NSError *error = nil;
NSData *jsonData = [NSData dataWithContentsOfURL:jsonUrl options:kNilOptions error:&error];
NSMutableDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:&error];
NSLog(@"%@",jsonResponse);

// MY ADDITIONS
NSArray *facultyMembers = [jsonResponse objectForKey:@"Faculty_Members"];
NSDictionary *facultyMember = [facultyMembers objectAtIndex:0];

or even

for(NSDictionary *dict in jsonResponse)
{
    // parse contents of dict; perhaps store in temp object and add to 
    // mutable dictionary or array
    NSNumber *ID = [dict objectForKey@"ID"];
    NSString *firstName = [dict objectForKey@"First_Name"];
    NSString *lastName = [dict objectForKey@"Last_Name"];
}

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.