Hi I have a file on a server json format with several map points:
["VALUE","proyect",[{"id":"1","name":"Frankie Johnnie & Luigo Too","address":"939 W El Camino Real, Mountain View, CA","lat":"37.386337","lng":"-122.085823"},morepoints..]]
I want to put these points as markers on my map in my ios app. I've looked at several pages and here but I have not found anything and what I have tried does not work.
Any help,please?
First, you first parse your JSON:
Second, you can then grab the array of locations out of that top-level array, create annotations for each location dictionary in that array, and add it to your map:
Having said that, I must say that I don't care for the JSON format:
It's a questionable design for us to have to divine what the first, second, and third objects in this array are. We shouldn't have to cryptically grab
array[2]
to get the array of locations.Arrays should be used for lists of equivalent items (e.g. the array of locations makes perfect sense). But this top level array is a little more dubious, with three very semantically different types of values, with nothing in the JSON to indicate what these three items are.
If you're stuck with this JSON format, then, fine, the above code should do the job. But hopefully you can change the top level structure here to be a dictionary, rather than an array, where you can use key names to identify the various items in this top-level dictionary, e.g.:
I didn't know what those first two values were supposed to be, so I just called them
id
andproject
, but you should obviously use names that truly reflect what those first two values are. But the key concept is to use a dictionary, where you can refer to the array of locations by name. For example, if the JSON was changed like I've suggested here, the code to parse it would become:Also, as a stylistic observation, many people would generally represent the latitude and longitude values as numbers (without the quotation marks), and the
NSJSONSerialization
would parse them asNSNumber
objects rather thanNSString
objects. But that's up to you.