Diese Seite mit anderen teilen ...

Informationen zum Thema:
Forum:
WinDev Forum
Beiträge im Thema:
7
Erster Beitrag:
vor 5 Monaten, 3 Wochen
Letzter Beitrag:
vor 2 Monaten, 3 Wochen
Beteiligte Autoren:
Geoff B, ccc2, Danny Lauwers, James Smiths, Ruben Sanchez Peña

[wm21 - android] how to use Deserialize() Json

Startbeitrag von ccc2 am 23.02.2017 12:31

Hi ,

due to lack of info in help , here I going to explain how to use Deserialize() json based on my try/error.

feel free to add if i missing anything
----------------------------------------------------------------------------------------

Deserialize( , , )

-> variable of a structure / variable array of a structure
-> must be buffer not string
-> psdJSON

test data1:
{"Id":1,"Name":"Product"}

Test data 2:
[{"Id":1,"Name":"Manager"},{"Id":3,"Name":"Staff"},{"Id":2,"Name":"Supervisor"}]

///// declare structure ///
stA is Structure
Id is int
Name is string
END



/////////////////////////////////////////////////////////////////////////////////////////
M_Res is buffer = HTTPGetResult() ---> return utf8 buffer


/////////////// program to retrieved test data1 //////////
M_stA is stA

Deserialize(M_stA, M_Res, psdJSON)

info(m_stA.Id, m_stA.Name)


/////////////// program to retrieved test data2 //////////
M_stA is array of stA
Deserialize(M_stA, M_Res, psdJSON)

FOR EACH item OF M_stA
info(item.Id, item.Name)
END


///////////////////////////////////////conclusion//////////////////////////////////////////////////////////
1. the [ ] in json is reason to use variable array rather than variable even if only one record

2. for some reason you may had converted json buffer to string , use stringtoutf8() to convert string to buffer before Deserialize








.

Antworten:

Thsnk you for the samples :)

Rubén

von Ruben Sanchez Peña - am 23.02.2017 13:02
Thank you very much, this is exactly what I was trying to do and works very well.

James

von James Smiths - am 12.05.2017 22:31
Any idea how to handle a complex json string...


{
"records": 2,
"customers": [
{ "name": "Liam",
"phoneNumbers": [
{ "type": "home",
"number": "012 3456"
},
{ "type": "work",
"number": "987 6543"
}
]
},
{ "name": "Diego",
"phoneNumbers": [
{ "type": "home",
"number": "123 4567"
},
{ "type": "work",
"number": "987 6543"
},
{ "type": "cell",
"number": "0412 987665"
}
]
}
]
}


von Geoff B - am 24.05.2017 11:01
Hi Geoff B ,

I don't understand why it's is complex. you need declare 3 structure and extract one at a time



stData is Structure
records is int
customers is string
END


stCustomer is Structure
name is string
phoneNumbers is string
END


stPhone is Structure
Type is string
number is string
END

von ccc2 - am 25.05.2017 06:53
Hi,

You need to build a top level structure with a number of records and an array of CustomerStructure.

This CustomerStructure itself has a name and an array of TelephoneStructure.

The TelefoonStructure has type and number defined.

The others are using the same strategy.

If using v22 you can use JsonToVariant to get a dynamically created structure of the JSON inside the variant.

Good luck !
Danny

von Danny Lauwers - am 26.05.2017 12:20
ccc2, ;-) well it was a bit more complex that the example you gave?

Thanks Danny, I may upgrade to 22 to get the JsonToVariant functionality.

von Geoff B - am 27.05.2017 07:50
Zur Information:
MySnip.de hat keinen Einfluss auf die Inhalte der Beiträge. Bitte kontaktieren Sie den Administrator des Forums bei Problemen oder Löschforderungen über die Kontaktseite.
Falls die Kontaktaufnahme mit dem Administrator des Forums fehlschlägt, kontaktieren Sie uns bitte über die in unserem Impressum angegebenen Daten.