PDA

View Full Version : JSON Parsing



Focus
13-Jan-2017, 07:34 AM
I appreciate it is very early days for the JSON documentation but I presume the class will support some sort of dot notation (xpath) or similar for getting at sub objects or will we have to keep testing jsonTypeObject and work our way through the tree ?

You know for getting at Thumbnail in this sort of structure



{
"Image": {
"Width": 800,
"Height": 600,
"Title": "View from 15th Floor",
"Thumbnail": {
"Url": "http://www.example.com/image/481989943",
"Height": 125,
"Width": 100
},
"Animated" : false,
"IDs": [116, 943, 234, 38793]
}
}

Harm Wibier
13-Jan-2017, 08:11 AM
There are no plans for adding support for XPath like notation to the parser itself.

Note sure what you mean by testing with jsonTypeObject. In this case you'd call GetMember twice and GetMemberValue once to get to the value (like the thumbnail URL) the quickest. If you are unsure if the JSON is of the expected format you can use HasMember.

Of course you can convert your JSON into a Struct using JsonToDataType and use the dot notation on that.

Focus
13-Jan-2017, 08:13 AM
Thanks for the clarification Harm

Focus
19-Jan-2017, 07:04 AM
Just for my own future reference as much as anything



Use cJsonObject.pkg

Struct tdI
Integer[] IDs // has to match JSON member name
End_Struct

Procedure ParseJSON
Handle hoJSON hoImage hoThumb
Boolean bTest bSuccess
String sVal sJSON
Integer iVal
Integer[] iaVal
tdI tI

Move '{"Image":{"Width":800,"Height":600,"Title":"View from 15th Floor","Thumbnail":{"Url":"http://www.example.com/image/481989943","Height":125,"Width":100},"Animated":false,"IDs":[116,943,234,38793]}}' to sJSON

Get Create (RefClass(cJSONObject)) to hoJSON
Get ParseString of hoJSON sJSON to bSuccess
If (bSuccess) Begin
Get IsOfJsonType of hoJSON jsonTypeObject to bTest
If (bTest) Begin
Get Member of hoJSON "Image" to hoImage
If (hoImage > 0) Begin
Get MemberValue of hoImage "Title" to sVal
Get MemberValue of hoImage "IDs" to sVal
Get JsonToDataType of hoImage to tI
Get Member of hoImage "Thumbnail" to hoThumb
If (hoThumb > 0) Begin
Get MemberValue of hoThumb "Url" to sVal
Get MemberValue of hoThumb "Height" to iVal
Send Destroy of hoThumb
End
Send Destroy of hoImage
End
End
End
Send Destroy of hoJSON
End_Procedure

Focus
19-Jan-2017, 07:24 AM
Or in one go ....




// note member names have to be an exact case match for JSON
Struct tdThumb
String Url
Integer Height
Integer Width
End_Struct

Struct tdImage
Integer Width
Integer Height
String Title
tdThumb Thumbnail
Boolean Animated
Integer[] IDs
End_Struct

Procedure ParseJSON
Handle hoJSON hoImage
Boolean bTest bSuccess
String sJSON
tdImage tImage

Move '{"Image":{"Width":800,"Height":600,"Title":"View from 15th Floor","Thumbnail":{"Url":"http://www.example.com/image/481989943","Height":125,"Width":100},"Animated":false,"IDs":[116,943,234,38793]}}' to sJSON

Get Create (RefClass(cJSONObject)) to hoJSON
Get ParseString of hoJSON sJSON to bSuccess
If (bSuccess) Begin
Get IsOfJsonType of hoJSON jsonTypeObject to bTest
If (bTest) Begin
Get Member of hoJSON "Image" to hoImage
If (hoImage > 0) Begin
Get JsonToDataType of hoImage to tImage
End
End
End
Send Destroy of hoJSON
End_Procedure
Send ParseJSON

Harm Wibier
20-Jan-2017, 08:49 AM
You do need to destroy hoImage..

starzen
23-Jan-2017, 03:55 PM
if you need xpath parsing you can download our free JSON parser at http://www.windowsdeveloper.com/dfPackage