iOS Need help parsing simple JSON in Xcode


macrumors 68000
Original poster
Dec 23, 2010
London, UK
Hi guys,

In the absence of any Xcode forum which I cannot find (if anyone can point me to one it would be much appreciated), I thought I'd post my problem here in the hope that someone on MacRumors is able to help. :D :D

I'm completely new to Xcode, C Language and basically very confused, and am pretty sure the help I require is very basic, so here goes:

Basically, I want to extract/parse the data from here (link) into a label on Xcode so that the label text displays the JSON data.

I've checked out a few tutorials and been searching google for 2 days but almost everything I see seems to deal with objects and not arrays in JSON. :(
As I understand, the link I've given is in JSON format (the documentation for using it states this at 4.2).

If anyone could provide me with a very simple and basic sample code with how to parse the data from the url and display it in a label I would HIGHLY APPRECIATE IT, this has been driving me nuts for days now and I would be grateful for any help.

Thank you once again. ;)

Edit: Here's a sample of the url for convenience (if you don't want to click the link):
[1,"Earls Court Square","74991","C3","Earl's Ct Tesco",1343942166000,1343942166000]
[1,"Earls Court Square","74991","C1","White City",1343942511000,1343942511000]
[1,"Earls Court Square","74991","74","Baker St Stn",1343942615000,1343942615000]
[1,"Earls Court Square","74991","74","Marble Arch",1343942858000,1343942858000]
[1,"Earls Court Square","74991","74","Marble Arch",1343942964000,1343942964000]
[1,"Earls Court Square","74991","C3","Earl's Ct Tesco",1343942660000,1343942660000]
[1,"Earls Court Square","74991","C1","White City",1343943155000,1343943155000]
[1,"Earls Court Square","74991","C3","Earl's Ct Tesco",1343943347000,1343943347000]
[1,"Earls Court Square","74991","C1","White City",1343943883000,1343943883000]
[2,"Earls Court Square","74991","8a56a0ab387d754001387fb596fa0ccf_29940",0,5,"Route N97 - From Saturday 21 July until Thursday 16 August route N97 will be unable to serve South Kensington Station with buses going direct via Cromwell Road instead. Passengers are advised to alight at the Victoria & Albert Museum instead.",1343968140000]


macrumors 68000
Original poster
Dec 23, 2010
London, UK
How do you propose to display multiple values, from the array, in a label, that takes a single value?
I intend to take a single value from the array and display it in a label. Sorry for not making it clear. I just need a simple example of extracting a value from the array and displaying it in the label.


macrumors 68000
Original poster
Dec 23, 2010
London, UK
So, you have the array already populated? What have you tried? Do you know about NSJSONSerialization?
I thought the response arrays are already populated with the live data? The values within the field of each array is the data afaik. Or do you mean something else by 'populated'? :confused:

I have tried quite a few sites after googling along the lines of parsing JSON in Xcode etc and have tried looking at quite a lot of help requests on parsing JSON on the StackOverflow website. But as was mentioned in my original post, I could only see sites explaining how to parse data from objects in JSON rather than arrays. In addition, I've followed this tutorial step-by-step with success whilst trying to understand how it works, but again, that also deals with a object-structured JSON data. I am sure it's not too far off, but I haven't a clue how to simply extract a value from an array and display it on a label. Any attempt I would make would almost certainly be completely wrong as I wouldn't know where to start, hence why I'm asking for a few simple lines of code. I am aware of the NSJSONSerialization class which has enabled native parsing of JSON in iOS 5.0+ and understand I have to use this.

I'm not asking to be spoon fed as I genuinely want to learn myself, but in this case, I'm requesting a simple/basic example code of how to get the values from the array and then display it on a label. I could then work from there getting an understanding of how it works.

Hope that made sense.

Thanks ;)


Staff member
Sep 2, 2004
The Centennial State
The data from that link is not in arrays though, really, is it? It's just text, with certain delimiters that can be used to parse it into arrays. Each line could be interpreted as a JSON array, but overall the entire feed is not valid JSON. So, when you say "array" are you referring to the values of each line of the feed? Or are you somehow wanting to process the entire feed as an array (of lines)?

I think you need to breakdown your issue into multiple steps, probably something like:
1) Parse (some of) the feed into an NSArray
2) Get a value from the array. Arrays are indexed. Which index into the array are you concerned about?
3) Convert the value, if necessary, into an NSString (since that is what UILabel's text property expects).
4) Assign the string to the label's text.

Does that sound like what you want?


macrumors 68040
Sep 2, 2008
What NSJSONSerialization and all of the third party JSON parsers do is take as input valid JSON and produce as output a data structure consisting of Foundation objects (like NSArray, NSDictionary, NSString and NSNumber). The JSON from this bus stop API seems to be an array of arrays.

Once you have the output from NSJSONSerialization it will be an NSArray. To get the second item in the array and then get one of the items inside that array you would use code like

NSString* second = [myJSONArray objectAtIndex:1];// [1,"Earls Court Square","74991","C3","Earl's Ct Tesco",1343942166000,1343942166000]
NSString* earlsCourt = [second objectAtIndex: 1];// "Earls Court Square"
To assign something to a label you use its text property:

myLabel.text = earlsCourt;
This is extremely basic so if you can't come up with code like this yourself you're trying to run before you can walk. You should start with a book on iOS development, work through it, and then come back to this project.

EDIT, dejo may be right that the results from the site are not valid JSON as is but rather are lines, each of which is a valid JSON array. This is kind of bizarre if so. I wasn't sure when I read it and maybe I'm wrong when I mention array of arrays above. If this is the case then it's more important that you get a firm understanding of iOS development before tackling this problem because I think you'll never figure it out (and I doubt that anyone here will take the time to figure it out for you).


macrumors newbie
Jul 10, 2012