-1

Hi Guys I am trying to parse the below JSON format into CLLocation.

 {
        "id": 6,
        "userId": 62,
        "name": "town run",
        **"locations": "(\n    \"<+32.09230720,+74.17861462> +/- 5.00m (speed -1.00 mps / course -1.00) @ 10/14/14, 6:38:22 PM Pakistan Standard Time\",\n    \"<+32.09231628,+74.17877018> +/- 5.00m (speed -1.00 mps / course -1.00) @ 10/14/14, 6:38:24 PM Pakistan Standard Time\",\n    \"<+32.09231628,+74.17889893> +/- 5.00m (speed -1.00 mps / course -1.00) @ 10/14/14, 6:38:27 PM Pakistan Standard Time\",\n    \"<+32.09231628,+74.17915642> +/- 5.00m (speed -1.00 mps / course -1.00) @ 10/14/14, 6:38:32 PM Pakistan Standard Time\",\n    \"<+32.09232083,+74.17967141> +/- 5.00m (speed -1.00 mps / course -1.00) @ 10/14/14, 6:38:34 PM Pakistan Standard Time\"\n)"**
 }

I want to parse "locations" into Array and then CLLocation. Please guide me how to resolve this issue.

Thanks in advance

Andrey Chernukha
  • 21,488
  • 17
  • 97
  • 161
  • locations into array and then cllocation? it's not quite clear what you want – Andrey Chernukha Oct 20 '14 at 14:23
  • I want to get lat, long, speed and timestamp from this string and that can only be done if we convert this string into an array and then into cllocation object to get the required values. – Muhammad Arslan Oct 20 '14 at 14:25

1 Answers1

1

Are you in control of creating this JSON in the first place? If so then the best solution would be to change how you are creating the JSON. The "locations" in this is a string that is designed to be displayed as text (i.e. in a website or something).

You would be better having locations be an array of dictionaries like this...

"locations" : [
    {
        "lat" : 32.09230720,
        "long" : 74.17861462,
        "accuracy" : 5.0,
        "speed" : -1.0,
        "course" : -1.0,
        "date" : "10/14/14, 6:38:22 PM Pakistan Standard Time"
    },
    {
        "lat" : 32.09230720,
        "long" : 74.17861462,
        "accuracy" : 5.0,
        "speed" : -1.0,
        "course" : -1.0,
        "date" : "10/14/14, 6:38:22 PM Pakistan Standard Time"
    },
    {
        "lat" : 32.09230720,
        "long" : 74.17861462,
        "accuracy" : 5.0,
        "speed" : -1.0,
        "course" : -1.0,
        "date" : "10/14/14, 6:38:22 PM Pakistan Standard Time"
    }
]

Once you have this you can convert to CLLocations very easily.

Essentially though you have to parse the string and find each separate part.

You can do something like...

NSArray *locations = [locationsString componentsSeparatedByString:@", "];

But this won't quite work due to the new lines and stuff.

Fogmeister
  • 76,236
  • 42
  • 207
  • 306
  • 1
    I have change the format from server response and make locations from string to array of dictionaries. – Muhammad Arslan Oct 22 '14 at 15:04
  • @user2699235 excellent. From a dictionary you can easily pull out the lat, long, etc... and use them to create the CLLocation objects. – Fogmeister Oct 22 '14 at 15:05