0

I am trying to import a JSON array and convert it to an NSArray for a UITableView, but can't work out how to grab the data.

My JSON is formatted as so when I use curl:

[{"location_name":"??"},{"location_name":"105"},{"location_name":"106"},{"location_name":"106A"},{"location_name":"106b"},{"location_name":"107"},{"location_name":"108"},{"location_name":"109"},{"location_name":"110"},{"location_name":"111"},{"location_name":"112"},{"location_name":"113"},{"location_name":"114"}]

My xCode code is as follows:

// Parse out the JSON data

NSError *jsonError;

NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&jsonError];

NSString* location_name = [json objectForKey:@"location_name"];

NSLog(@"Location Name: %@", location_name);

But in my console output I only get: "Location Name: (null)"

5
  • 2
    Have you tested the jsonError variable? That might give you some hints as to what is going on. Commented Feb 16, 2014 at 14:18
  • First, the JSON you have supplied is an array of objects, so using [json objectForKey:@"location_name"] will not produce anything. If you log out json what does it produce? Commented Feb 16, 2014 at 14:19
  • Are you sure you have given all the details here? The code you have should crash and not print Location Name: (null) Commented Feb 16, 2014 at 14:19
  • Go to json.org and spend the 5 minutes it takes to learn JSON syntax. What you have is an array of "objects" (dictionaries), not a single object/dictionary. And as @68cherries says, you should print the error parm, since it appears that the JSON did not convert at all. Commented Feb 16, 2014 at 14:27
  • Ok, wow, thanks, there are so many answers so quickly! I will work through, these make a lot of sense and I am sure they will fix it, thanks for your help, i'll report back with what works! Commented Feb 16, 2014 at 14:31

4 Answers 4

2

Your root json object is not a dictionary but an array:

[{"location_name":"??"},{"location_name":"105"},{"location_name":"106"},{"location_name":"106A"},{"location_name":"106b"},{"location_name":"107"},{"location_name":"108"},{"location_name":"109"},{"location_name":"110"},{"location_name":"111"},{"location_name":"112"},{"location_name":"113"},{"location_name":"114"}]

As a result, here is how you can display its content:

NSError *e = nil;
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error: &e];

if (!jsonArray) {
  NSLog(@"Error parsing JSON: %@", e);
} else {
   for(NSDictionary *item in jsonArray) {
      NSLog(@"Item: %@", item);
   }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Using this I am getting an error: "{NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}" I have tried allowing fragments but then get "{NSDebugDescription=Invalid value around character 0.}", Any ideas? Just googling up on it now.
*Its working! My PHP file had this line: print_r($result); Which must have been interfering, removed that and data is downloaded. Thanks so much!
1

Your JSON contains an array of dictionaries, thus do:

NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:data 
                                                     options:NSJSONReadingMutableContainers 
                                                       error:&jsonError];

Then you can loop through the array (or pick the one according to the indexPath of your tableView) to get a single location.

Comments

0

You're json structure is an Array containing several object. When you parse it using the NSJSONSerialization, you get an NSArray back, not a NSDictionnary. So you probably need of a for loop!

NSError *jsonError;

NSArray *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&jsonError];

for(NSDictionnary * item in json)

{

NSString* location_name = [item objectForKey:@"location_name"];

NSLog(@"Location Name: %@", location_name);

}

Comments

0

If you literally just want to create an array of location_name's then you should change objectForKey: to valueForKey:, which will return you the array you want.

NSArray *locationNames = [json valueForKey:@"location_name"];

The reason this works is because you have an array of dictionaries. If you call valueForKey: on an array it will collect the result of calling valueForKey: on each of it's elements

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.