JSON string will not parse

I am trying to parse a JSON string from a web service. The line that appears is as follows:

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

My iOS code is as follows:

 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 correct JSON data will be displayed in the log, but I keep getting "NOPE" in isValidJsonObject.

The web service sends the data back as a string data type. Does it matter? If so, what type of data should I send?

Any ideas would be much appreciated!

+4
source share
3 answers

You are not using isValidJSONObject: to validate a valid JSON string, you use it to validate an object that can be converted to JSON; See the documentation :

isValidJSONObject:

Gets a boolean value indicating whether this object can be converted to JSON data.

 + (BOOL)isValidJSONObject:(id)obj 

Parameters:

obj
Object for testing.


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 NSError to error .

+3
source

It is also possible that the web service is delivering JSON strings in the wrong encoding.

According to the NSJSONSerialization class Help !

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. Data may or may not have a specification. The most efficient encoding for parsing is UTF-8, so if you have a choice in encoding the data passed to this method, use UTF-8.

0
source

You must read 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 can 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"]; } 
0
source

Source: https://habr.com/ru/post/1398827/


All Articles