JSON on iOS

I have this JSON response and would like to store the values of ‘artist name’ and ‘album title’ in two text variables. How would I do that?

Here the tutorial

Thanks Robert, but that’s in ObjC, which I don’t speak :slight_smile:

http://developer.xojo.com/xojo-data
You get a dictionary object out after calling ParseJSON that you can then query for the properties you are interested in.

You’ll start with something like this:

  Dim jsonDict As xojo.Core.Dictionary
  jsonDict = Xojo.Data.ParseJSON(kJSON) // kJSON is your JSON text
  
  Dim releases() As Auto = jsonDict.Value("releases")
  For Each release As xojo.Core.Dictionary In releases
    Dim title As Text = release.Value("title") // "Black Ice"
  Next

And then fetch the values you want out of the Dictionary.

Here is the JSON webinar for more help:

Thanks Paul, that seems to do the job. At least for the ‘album title’. However, when I add the next items, I get Key Not Found errors. Any idea why? Here’s my code:

Yes. “name” is not in that part of the JSON. I don’t see where “error” is at all.

Try viewing your JSON using JSON Formatter to view the structure better. You’ll see that artist is in the artist-credit object, so you’d have to get that first using something like this:

Dim artistCredit As Xojo.Core.Dictionary = release.Value("artist-credit") Dim artist As Xojo.Core.Dictionary = artistCredit.Value("artist") Dim name As Text = artist.Value("name")

Thanks but I’m afraid I’m getting a nil object exception, right in the first line you suggested. I tried nesting with more for-each statements but that didn’t work either.

Dim socket1 As New HTTPSocket
Dim jsonData As String = socket1.Get("http://musicbrainz.org/ws/2/release/?query=barcode:602498739532&fmt=json", 30)
Dim apiResponse as new JSONItem(jsonData)
  
Dim s As Text
s = jsonData.ToText
  
Dim jsonDict As xojo.Core.Dictionary = Xojo.Data.ParseJSON(s) // s is my JSON text
  
Dim releases() As Auto = jsonDict.Value("releases")
  For Each release As xojo.Core.Dictionary In releases
    Dim AlbumTitle As Text = release.Value("title") // "Black Ice"
    Dim artistCredit As xojo.Core.Dictionary = release.Value("artist-credit")
    Dim artist As Xojo.Core.Dictionary = artistCredit.Value("artist")
    Dim name As Text = artist.Value("name")
  next

It is important to understand the JSON. In this case, I only quickly looked at it and did not realize that “artist-credit” is an array, not an object. Again, I used JSON Formatter to understand the structure. The code would only be slightly different to handle the array:

[code] Dim jsonDict As xojo.Core.Dictionary = Xojo.Data.ParseJSON(jsonText)

Dim albumTitle, artistName As Text
Dim releases() As Auto = jsonDict.Value(“releases”)
For Each release As xojo.Core.Dictionary In releases
albumTitle = release.Value(“title”) // “Black Ice”

// artist-credit is an array
Dim artistCredits() As Auto = release.Value("artist-credit")
Dim artistCredit As xojo.Core.Dictionary = artistCredits(0)

Dim artist As Xojo.Core.Dictionary = artistCredit.Value("artist")
artistName = artist.Value("name")

Next

OutputArea.Text = albumTitle + " by the " + artistName[/code]

Still, you are going to have to understand the entire JSON structure in order to parse it properly. Programming by guessing leads to much pain.

Here’s an iOS project with this code and the HTTSocket code.

Great Paul. I did look at the JSON formatter and your webinar but somehow missed the array clue. Thanks for helping me out.

It doesn’t help that this JSON is using arrays for items that only have one element. Perhaps they sometimes have multiple elements, but it does make it easy to miss in this situation!