PDA

View Full Version : Need help parsing simple JSON in Xcode




Hustler1337
Aug 2, 2012, 04:17 PM
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 (http://countdown.api.tfl.gov.uk/interfaces/ura/instant_V1?StopCode1=50224&DirectionID=1&VisitNumber=1&ReturnList=StopCode1,StopPointName,LineName,DestinationText,EstimatedTime,MessageUUID,MessageText,Me ssagePriority,MessageType,ExpireTime)) 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 (http://www.tfl.gov.uk/assets/downloads/businessandpartners/tfl-live-bus-arrivals-api-documentation.pdf) 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):
[4,"1.0",1343942093294]
[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]



dejo
Aug 2, 2012, 04:38 PM
How do you propose to display multiple values, from the array, in a label, that takes a single value?

Hustler1337
Aug 2, 2012, 04:52 PM
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.

dejo
Aug 2, 2012, 05:05 PM
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.

So, you have the array already populated? What have you tried? (http://mattgemmell.com/2008/12/08/what-have-you-tried/) Do you know about NSJSONSerialization (http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html)?

Hustler1337
Aug 2, 2012, 06:27 PM
So, you have the array already populated? What have you tried? (http://mattgemmell.com/2008/12/08/what-have-you-tried/) Do you know about NSJSONSerialization (http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html)?

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 (http://www.raywenderlich.com/5492/working-with-json-in-ios-5) 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 ;)

dejo
Aug 2, 2012, 07:37 PM
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 (http://www.json.org). 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?

PhoneyDeveloper
Aug 2, 2012, 07:47 PM
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).

ahan.tm
Aug 2, 2012, 09:31 PM
Ray Wenderlich has an excellent tutorial about using JSON in iOS5

Check it out: http://www.raywenderlich.com/5492/working-with-json-in-ios-5

SplitIt!
Aug 3, 2012, 09:00 PM
Ray Wenderlich has an excellent tutorial about using JSON in iOS5

Check it out: http://www.raywenderlich.com/5492/working-with-json-in-ios-5

For simple JSONs the above post is an excellent example.

If you are developing a cloud based app, check out RestKit on GitHub (https://github.com/RestKit/RestKit).

RestKit allows you to directly map the JSON returned from the server into CoreData backed NSManagedObjects in your App. It is an amazingly powerful library.