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!

NeedSomeAnswers
  • 53
  • 1
  • 2
  • 8
  • i am try to parese same type of dic. and getting problem. @ NeedSomeAnswers if you get any solution and post it. – Ayaz Nov 25 '13 at 13:12

3 Answers3

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.

Ry-
  • 218,210
  • 55
  • 464
  • 476
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"];
}
Ri_
  • 662
  • 8
  • 13
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.

motou
  • 726
  • 4
  • 12
  • 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. – NeedSomeAnswers Feb 28 '12 at 15:12
  • Can I encode is on the IOS side, or do i have to do it on the web service side? – NeedSomeAnswers Feb 28 '12 at 15:14