PDA

View Full Version : JsonToDataType VS Struct



Salvadori
27-Aug-2018, 01:26 PM
I'm trying to get a struct from json object, but the work is harder than I expected



Function ResponseCardFromJSON Handle hoResponse Returns Panorama_tPedido
Boolean isParseOK
String StringVariable sJson
Handle hoJSON
Panorama_tPedido responseCard //Panorama_tPedido is my base struct

Send RemoveEmptyMembers hoResponse True True True //remove Null values

Get Create (RefClass(cJsonObject)) to hoJSON

Get ParseString of hoJSON (Stringify(hoResponse)) to isParseOK

Set pbRequireAllMembers of hoJson to False

If (isParseOK) Begin
Get JsonToDataType of hoJSON hoResponse to responseCard <-- error line

Get psParseError of hoJSON to StringVariable
End

Send Destroy to hoJSON
Send Destroy to hoResponse

Function_Return responseCard
End_Function


I added in attachment my json (JsonTest.txt) and my structures (generated by Unicorn InterGlobal's RestGen program)

the error i got is "Invalid Json Object Expected number!"

can anyone help me understand what is going on with my json data? thanks you

12220

Salvadori
27-Aug-2018, 02:21 PM
I made it work with:




Direct_Input "Z:\AppHtml\JsonTest.txt"
Read_Block ucaJson -1
Close_Input

Get Create (RefClass(cJsonObject)) to hoJson
Get ParseUtf8 of hoJson ucaJson to bOK

Send RemoveEmptyMembers hoJson True True True
Set pbRequireAllMembers of hoJson to False
Get JsonToDataType of hoJson to tOrdDat



but it's still a little confusing for me :/

Mike Peat
28-Aug-2018, 04:22 AM
Leonardo

What is confusing? ;)

You:

Read a JSON file into memory (your UChar array of bytes)
Create a JSON object
Read the UChar data into that (ParseUTF8)
Get rid of any nulls (I assume) and empty elements in the JSON
Do not require that all elements of the struct be present in the JSON (they rarely are, in my experience)
Export the JSON into your struct variable


Mike

Salvadori
28-Aug-2018, 07:22 AM
thanks you Mike, my big problem was that the response of API sometimes comes between [ and ], it took me a while to figure it out, In the meantime i kept changing my functions, that means causing more problems :(

thanks for your step-by-step It kept me in my focus.

Salvadori
30-Aug-2018, 02:54 PM
Hello, now i understand my api response, it's a array, so how can i convert my json into a struct?

now i got this code, all works good when the api response isn't a array



Function fJSONtoStructPedido Handle hoResponse Returns Panorama_tPedido
Boolean isParseOK
String sJson
Handle hoJSON
Panorama_tPedido tPedido

Move (Stringify(hoResponse)) to sJson
Move (Right(sJson,(Length(sJson)-1))) to sJson
Move (Left(sJson,(Length(sJson)-1))) to sJson

Get Create (RefClass(cJsonObject)) to hoJSON

Get ParseString of hoJSON sJson to isParseOK

Send RemoveEmptyMembers hoJSON True True True

Set pbRequireAllMembers of hoJson to False

If (isParseOK) Begin
Get JsonToDataType of hoJSON to tPedido
End
Else Begin
// ToDo: error
End

Send Destroy to hoJSON
Send Destroy to hoResponse

Function_Return tPedido
End_Function


if i keep the "[" "]" characters who i remove when get the response, i got this error "Invalid Json Object Expected JSON object!"

i aready tried to use my struct as an array "Panorama_tPedido[] tPedido" but unsuccess test :/

anybody know how to deal with that? (json arrays)

thanks

Jose
31-Aug-2018, 03:02 AM
Hello Salvadori,

I am using successfully JsonToDataType to move a JSON handle object to a struct.
Has your JSON deepper objects or arrays?



// Struct definition
Struct tServices
String code
String description
End_Struct

// Variable declaration
tServices[] Services

// Calling JsonToDataType (hoJson is returned by HttpPostJson (cJsonHttpTransfer))
Get JsonToDataType of hoJson to Services

// Example of JSON returned
[
{
"code": "code1",
"description": "description1",
},
{
"code": "code2",
"description": "description2",
}
]


Regards.

Salvadori
31-Aug-2018, 06:16 AM
Hello Salvadori,

I am using successfully JsonToDataType to move a JSON handle object to a struct.
Has your JSON deepper objects or arrays?



// Struct definition
Struct tServices
String code
String description
End_Struct

// Variable declaration
tServices[] Services

// Calling JsonToDataType (hoJson is returned by HttpPostJson (cJsonHttpTransfer))
Get JsonToDataType of hoJson to Services

// Example of JSON returned
[
{
"code": "code1",
"description": "description1",
},
{
"code": "code2",
"description": "description2",
}
]


Regards.

:cool: i don't know why i'm always goes for the hard way, very thanks you @Jose

Final code (i hope so)




Function fJSONtoStructPedido Handle hoResponse Returns Panorama_tPedido
Panorama_tPedido[] tPedido

Send RemoveEmptyMembers hoResponse True True True//
Set pbRequireAllMembers of hoResponse to False
Get JsonToDataType of hoResponse to tPedido

Function_Return tPedido
End_Function

Mike Peat
31-Aug-2018, 09:08 AM
Leonardo

Do Get JsonToDataType of hoJson to an array of the right kind of struct.

Mike