Re: JsonToDataType VS Struct
I made it work with:
[CODE]
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
[/CODE]
but it's still a little confusing for me :/
Re: JsonToDataType VS Struct
Leonardo
What is confusing? ;)
You:
[LIST=1][*]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[/LIST]
Mike
Re: JsonToDataType VS Struct
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.
Re: JsonToDataType VS Struct
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
[CODE]
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
[/CODE]
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
Re: JsonToDataType VS Struct
Hello Salvadori,
I am using successfully JsonToDataType to move a JSON handle object to a struct.
Has your JSON deepper objects or arrays?
[CODE]
// 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",
}
]
[/CODE]
Regards.
Re: JsonToDataType VS Struct
[QUOTE=Jose;337339]Hello Salvadori,
I am using successfully JsonToDataType to move a JSON handle object to a struct.
Has your JSON deepper objects or arrays?
[CODE]
// 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",
}
]
[/CODE]
Regards.[/QUOTE]
:cool: i don't know why i'm always goes for the hard way, very thanks you @Jose
Final code (i hope so)
[CODE]
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
[/CODE]
Re: JsonToDataType VS Struct
[COLOR=#333333]Leonardo
Do Get JsonToDataType of hoJson to an array of the right kind of struct.
Mike[/COLOR]