Web: JSON - How to get at the values of this example?

I’m playing around with JSON. I found this OData Example, and trying to import it. But there are more informations than in a usual JSON?

Source: http://services.odata.org/V3/OData/OData.svc/Products?$format=json

{"odata.metadata":"http://services.odata.org/V3/OData/OData.svc/$metadata#Products","value":[{"ID":0,"Name":"Bread","Description":"Whole grain bread","ReleaseDate":"1992-01-01T00:00:00","DiscontinuedDate":null,"Rating":4,"Price":2.5},{"ID":1,"Name":"Milk","Description":"Low fat milk","ReleaseDate":"1995-10-01T00:00:00","DiscontinuedDate":null,"Rating":3,"Price":3.5},{"ID":2,"Name":"Vint soda","Description":"Americana Variety - Mix of 6 flavors","ReleaseDate":"2000-10-01T00:00:00","DiscontinuedDate":null,"Rating":3,"Price":20.9},{"ID":3,"Name":"Havina Cola","Description":"The Original Key Lime Cola","ReleaseDate":"2005-10-01T00:00:00","DiscontinuedDate":"2006-10-01T00:00:00","Rating":3,"Price":19.9},{"ID":4,"Name":"Fruit Punch","Description":"Mango flavor, 8.3 Ounce Cans (Pack of 24)","ReleaseDate":"2003-01-05T00:00:00","DiscontinuedDate":null,"Rating":3,"Price":22.99},{"ID":5,"Name":"Cranberry Juice","Description":"16-Ounce Plastic Bottles (Pack of 12)","ReleaseDate":"2006-08-04T00:00:00","DiscontinuedDate":null,"Rating":3,"Price":22.8},{"ID":6,"Name":"Pink Lemonade","Description":"36 Ounce Cans (Pack of 3)","ReleaseDate":"2006-11-05T00:00:00","DiscontinuedDate":null,"Rating":3,"Price":18.8},{"ID":7,"Name":"DVD Player","Description":"1080P Upconversion DVD Player","ReleaseDate":"2006-11-15T00:00:00","DiscontinuedDate":null,"Rating":5,"Price":35.88},{"ID":8,"Name":"LCD HDTV","Description":"42 inch 1080p LCD with Built-in Blu-ray Disc Player","ReleaseDate":"2008-05-08T00:00:00","DiscontinuedDate":null,"Rating":3,"Price":1088.8},{"odata.type":"ODataDemo.FeaturedProduct","ID":9,"Name":"Lemonade","Description":"Classic, refreshing lemonade (Single bottle)","ReleaseDate":"1970-01-01T00:00:00","DiscontinuedDate":null,"Rating":7,"Price":1.01},{"odata.type":"ODataDemo.FeaturedProduct","ID":10,"Name":"Coffee","Description":"Bulk size can of instant coffee","ReleaseDate":"1982-12-31T00:00:00","DiscontinuedDate":null,"Rating":1,"Price":6.99}]}

I can load it via httpsocket, save this to a string, but the point how I separate the JSON from the rest is difficult, can the JSON Parse find himself the dictionarys in such a complex example?

Have you tried? If it doesn’t work then you need to parse the Json yourself. Doesn’t look really complicated.

There is nothing wrong with this json. Looks like it’s a straigh translation from an xml structure actually. What you’ve got here is an element named odata.metadata whose “value” element contains an array of json items.

Something like this can easily loop through the values:

  Dim jsonDict As Xojo.Core.Dictionary
  jsonDict = Xojo.Data.ParseJSON(kJSON) // Your JSON
  
  Dim metaData As Text = jsonDict.Value("odata.metadata")
  
  // The use of [] for the value indicates it is an array
  Dim values() As Auto = jsonDict.Value("value")
  
  // Loop through the array of values
  For i As Integer = 0 To values.Ubound
    // Assign each value to a Dictionary so you can get its contents
    Dim valueDict As Xojo.Core.Dictionary = values(i)
    
    // Grab the entries that you want
    Dim name As Text = valueDict.Value("Name")
    Dim description As Text = valueDict.Value("Description")
    
    // And so on..
    MsgBox(name + ": " + description)
  Next 

Thank you! Now I understand the way.

I had problems with the

"{“odata.metadata”:"http://services.odata.org/V3/OData/OData.svc/$metadata#Products",“value”:[{"

So I began to find the len, replaced it with a “”. Your solution is much easier, than I ever thought.

Thanks.