JSON parse

Just getting started with XOJO, and I’m having trouble figuring out how to parse some JSON data. Any help would be appreciated.

JSON:

[ { "id": 530, "type": "Asset", "assetNumber": "W-D-7154", "macAddress": "00:00:00:00:00:00", "networkName": "W-D-7154", "model": { "id": 340, "type": "Model", "modelName": "Compaq dc7800 ", "manufacturer": { "id": 16, "type": "Manufacturer" } } }, { "id": 2605, "type": "Asset", "assetNumber": "W-L-007", "macAddress": "00:00:00:00:00:00", "networkName": "W-L-007", "model": { "id": 519, "type": "Model", "modelName": "Surface Book", "manufacturer": { "id": 48, "type": "Manufacturer" } } }, { "id": 734, "type": "Asset", "assetNumber": "W-L-7003", "macAddress": "00:00:00:00:00:00", "networkName": "W-L-7003", "model": { "id": 5, "type": "Model", "modelName": "TECRA A11", "manufacturer": { "id": 5, "type": "Manufacturer" } } }, { "id": 1677, "type": "Asset", "assetNumber": "W-L-7102", "macAddress": "00:00:00:00:00:00", "networkName": "W-L-7102", "model": { "id": 5, "type": "Model", "modelName": "TECRA A11", "manufacturer": { "id": 5, "type": "Manufacturer" } } }, { "id": 620, "type": "Asset", "assetNumber": "W-L-9182", "macAddress": "00:00:00:00:00:00", "networkName": "W-L-9182", "model": { "id": 49, "type": "Model", "modelName": "OptiPlex 790", "manufacturer": { "id": 3, "type": "Manufacturer" } } } ]

Hacked together code modified from “JSON web feed app” example:

[code]If HTTPStatus = 200 Then
Dim jsonText As Text = Xojo.Core.TextEncoding.UTF8.ConvertDataToText(Content)

Dim jsonArray() As Auto = Xojo.Data.ParseJSON(jsonText)

For Each jsonDict As Xojo.Core.Dictionary In jsonArray

FeedList.DeleteAllRows

// Display the feed title
//FeedTitleLabel.Text = jsonDict.Value("title")

// Display the feed articles
Dim items() As Auto = jsonDict.Value(0)
For Each article As Xojo.Core.Dictionary In items
  Dim id As Text = article.Value("id")
  Dim assetNumber As Text = article.Value("assetNumber")
  FeedList.AddRow(id, assetNumber)
  
  // Save article content
  //FeedList.RowTag(FeedList.LastIndex) = article.Value("content_html")
Next

Next

End If[/code]

The parse looks fine to me, but it looks like the data extraction is for another dataset.

Get rid of the inner for each loop and just set article thusly:

dim article as Xojo.core.dictionary = jsondict.value(“model”)

Thanks for the help. Figured out breakpoints and was able to step through and see the iterations.

Now I am getting a HTTPStatus of 400 on the get request. I can copy & paste the URL into web browser and get a valid JSON reponse, with a 200 status.

Any idea where I should look to figure this out?

I’m testing on Win 10. Maybe I’ll flip over to the Mac side and see if it changes anything.

400 indicates a bad request. You are probably missing some headers or url part.

Try header “Accept” with a value of “text/html” perhaps?

I had similar problems with the “old” HttpSecureSocket being HTTP/1.0. The API I was using required HTTP/1.1 as protocol. Switching to the new framework Socket or using Curl was the solution.

UGH! After trying all the suggestions here and elsewhere, I decided to change the quotes in the url from double to single.

Worked like a champ.

Why double quotes work in ANY web browser and the fabulous “Postman” rest app and NOT Xojo, I’ll probably never know. Thanks for all the suggestions.

How where your quotes setup? I can’t see a use for quotes inside a URL

Right. Quotes should be URLEncoded, and the browser does that for you.

Yeah, I should know better. URL I was working off of was similar to:

https://localhost/helpdesk/WebObjects/Helpdesk.woa/ra/Clients?qualifier=(firstName%20like%20’Rich*’)%20and%20(location.locationName%3D’PES’)&apiKey=v32lXMFAi7dl3zGrtETArXqKVF8svfAfXZpIwC0P