0

I got a string with a particular format which have a Entity Name:@"Entry". I can't modify the string. How to access the string? I can access array objects but not inside. How to use initWithEntityName?

Here is the string

<__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;
})

This is how I got the array.

+(NSArray*) routePlan
{
NSString* jsonString = [NSString stringWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"Documents/DataJson" withExtension:nil]
                                                        encoding:NSUTF8StringEncoding error:nil];
NSArray* array = [jsonString componentsSeparatedByString:@","];


}

Do I need to use predicate?

I need the value of latitude and logtude. I can do "po array[0]", but can't go inside of array[0].

Get the below error if I try to access latitude.

error: Execution was interrupted, reason: Attempted to dereference an invalid ObjC Object or send it an unrecognized selector.
The process has been returned to the state before expression evaluation.

If I do po array[0], I got the below.

<__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;
})

2 Answers2

0

First of all, your file is not in json format. Now, when you do this :

NSString* aircraftJSONString = [NSString stringWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"Documents/AircraftDataJson" withExtension:nil]
                                                        encoding:NSUTF8StringEncoding error:nil];
NSArray* aircraftJsonFplWaypoints = [aircraftJSONString componentsSeparatedByString:@","];

You are actually splitting your text at the comma, that's why your aircraftJsonFplWaypoints contains 2 object : Fist a string with value :

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

then a string with value

(entity: Entry; id: 30506398-1852-433D-B536- DC57F484F754> ; data: { cumulativeTime = 0000; latitude = “13.067439” longitude = "80.237617"; type = enrty; entryName = Chennai; })

What you have in aircraftJsonFplWaypoints are strings, not dictionary nor array. This will lead you nowhere.

What you need to do is to use regular expression to get what's in the {} . This should work:

{[^}]*}

So I'll do something like :

    NSError * error;
    NSString * pattern = @"\\{[^\\}]*\\}";
    NSRegularExpression * regex = [[NSRegularExpression alloc] initWithPattern:pattern
                                                                       options:0
                                                                         error:&error];

    NSArray<NSTextCheckingResult *> * matches = [regex matchesInString:aircraftJSONString options:NSMatchingReportCompletion range:NSMakeRange(0, aircraftJSONString.length)];

    for(NSTextCheckingResult * match in matches)
    {
        NSString * substring = [aircraftJSONString substringWithRange:match.range];
        // Remove the bracket
        substring = [substring substringWithRange:NSMakeRange(1, substring.length - 2)];

        // split around the ";" => get the llat/long

        NSArray<NSString *>* parts = [substring componentsSeparatedByString:@";"];
        NSString * latLong = parts[1];

        /* continue to get the longitude and latitude */
    }
}

but I would recommend to use a standard for input, for example instead of copy/paste the po output, you can serialize your objects to json with the NSJSONSerialisation API with would make storing/retrieving much easier.

Florian Burel
  • 3,408
  • 1
  • 19
  • 20
0

It seems to be the data you are fetching from bundle file is not a valid JSON, it is something else. If it is the JSON data then the below will be the approach to get that.

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"DataJson" ofType:@"json"];
NSLog(@"%@",filePath);
NSURL *url = [NSURL fileURLWithPath:filePath];
NSData *jsonData = [NSData dataWithContentsOfURL:url];
NSError *error = nil;
id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableLeaves error:&error];
if (error == nil){
    NSLog(@"#### JSON object #### %@",jsonObject);
}else{
    NSLog(@"#### JSON parsing error #### %@",error);
}

But as you mentioned you have an Entry as ManagedObject, which means you are trying to fetch the data from core data. So that please confirm me how you are saving and fetching data from coredata. (provide me sample code).

If you feel that you are correct with your approach, then please try to print the Entry object properties.

   +(NSArray*) routePlan
    {
        NSString* jsonString = [NSString stringWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"Documents/DataJson" withExtension:nil]
                                                        encoding:NSUTF8StringEncoding error:nil];
        NSArray* array = [jsonString componentsSeparatedByString:@","];

        [array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger index, BOOL * _Nonnull stop)
         {
             Entry* entry = (Entry*)obj;
             NSLog(@"id is :: %@",entry.id);
             NSLog(@"id is :: %@",entry.data);
         }];
        return nil;
    }
Jayachandra A
  • 1,335
  • 1
  • 10
  • 21