0

This is my "po array" looks like.

<__NSArrayI 0x7fe093f87160>(
<Entry: 0x600000498c90> (entity: Entry; id: 30506398-1852-433D-B536- 
DC57F484F754> ; data: {
cumulativeTime = 0000;
latitude = “12.972442”
longitude = "77.580643";
type = enrty;
entryName = Bangalore;
}),
<Entry: 0x600000498c90> (entity: Entry; id: 30506398-1852-433D-B536- 
DC57F484F754> ; data: {
cumulativeTime = 0000;
latitude = “13.067439”
longitude = "80.237617";
type = enrty;
entryName = Chennai;
})

The above JSON is stored in and I'm retrieving using the below code.

 +(NSArray*) routePlan
{
     NSString* aircraftJSONString = [NSString stringWithContentsOfURL [[NSBundle mainBundle] URLForResource:@"Documents/DataJson" withExtension:nil]

encoding:NSUTF8StringEncoding error:nil];
NSArray* aircraftJsonFplWaypoints = [aircraftJSONString componentsSeparatedByString:@","];
}

I can access the array[0] but not the objects in array[0]. I need the latitude and longitude. Any idea how to strip down to the inside dictionary values.

7
  • get array[0] as dictionary than get value of the key from dictionary. Commented Mar 28, 2018 at 9:12
  • CAN YOU SHARE SOME CODE? Commented Mar 28, 2018 at 9:34
  • 1
    This can never be the array in the question. By the way: Please don't use the comments to add information. Edit your question. Commented Mar 28, 2018 at 9:41
  • Any idea how to solve it? Commented Mar 28, 2018 at 9:47
  • @HirakBarman check my answer Commented Mar 28, 2018 at 9:53

4 Answers 4

2

The dictionaries seem to be instances of an NSManagedObject subclass named Entry.

Just use a loop to iterate over the entries

for (Entry *entry in array) {
    NSLog(@"lat: %@ - long: %@", entry.latitude, entry.longitude);

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

4 Comments

No luck. Still crash.
You are talking about two different things. The po array mentioned in the question and the array you got from the JSON string. The former is an array of Entry instances and the latter is an array of String. There is no dictionary involved. Usually JSON is supposed to be deserialized with NSJSONSerialization.
Sorry. I'm new to dev and got stuck on this issue.
Sorry, the question cannot be answered with the current information. Please add more information for example the JSON and what you are going to accomplish.
0

You have an array of dictionaries and you need to parse through it.

I haven't tried running this code but this should work. Later on you can add some safety checks for nil values.

NSDictionary *dict = array[0];
NSString *latitude = dict["latitude"];
NSString *longitude = dict["longitude"];

Comments

0

You can use key value coding :

[array[0] valueForKey:@"latitude"];

Comments

0

Simply use objective c fast enumeration for getting data from ArrayList.

for (NSDictionary *dic in array) {
    NSLog(@"dic values %@",[dic objectForKey:@"data"]);

    NSDictionary *data = [dic objectForKey:@"data"];

    NSString *lat = [data objectForKey:@"latitude"];

}

5 Comments

Does not work. *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x7fdc427a7ab0'
@HirakBarman try to get [dic objectForKey:@"data"]. if you are getting then let me know.
Nope getting crash.
@HirakBarman can you share source from where this data is getting or share your code
@HirakBarman check your array content weather is it properly in JSON format or not. you can use online tools for JSON format validation.

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.