Diese Seite mit anderen teilen ...

Informationen zum Thema:
Forum:
WinDev Forum
Beiträge im Thema:
5
Erster Beitrag:
vor 2 Jahren, 3 Monaten
Letzter Beitrag:
vor 2 Jahren, 3 Monaten
Beteiligte Autoren:
ech madriaga, Danny Lauwers, Arekusei Timakobu

JSON Data Structure

Startbeitrag von ech madriaga am 24.11.2015 06:22

Hi All,

I need your advice.
How I can change/modify the original or current output number 1 to the structure in Number 2. I need to put and "Enter Command" after "[" and before "]".

1. Original Output - [{"id":1,"type":"Model","modelName":"Model Name","assettype":{"id":1,"type":"AssetType"},"manufacturer":{"id":1,"type":"Manufacturer"},"warrantyType":{"id":1,"type":"WarrantyType"}}]

2. Final Output - [
{
"id":1,
"type":"Model",
"modelName":"Model Name",
"assettype":{"id":1,"type":"AssetType"},
"manufacturer":{"id":1,"type":"Manufacturer"},
"warrantyType":{"id":1,"type":"WarrantyType"}
}
]

Here's my code:
myRequest is restRequest
myResponse is restResponse
vResvar is Variant
myRequest.URL = "http://localhost:8081/helpdesk/WebObjects/Helpdesk.woa/ra/Models?&username=admin&password=admin";

//myRequest.Method = httpPost
// just part of json sample, make it yourself right as a complete json


myResponse = RESTSend(myRequest)

Info(myResponse.Content)
IF ErrorOccurred THEN
Error(ErrorInfo(errFullDetails))
ELSE

vResvar=JSONVersVariant(myResponse.Content)

POUR TOUT Models DE vResvar.results

TableAjouteLigne(TABLE_NoName1,Models .id,Models .modelName)
FIN
//Info(myResponse.Content)

END

But its giving me an error:
Le variant n'est pas un objet
Code erreur : 2250

I think the problem is the current structure of the JSON. I need to change it to Number 2.
But how?

Thanks,

Antworten:

Your JSON represents an object but objectes a not supportted by JSONToVariant/VariantTOJSON. Try to convert object to associative array - remove framing [].

von Arekusei Timakobu - am 25.11.2015 07:00
Hi Arekusei,

Can you help me about this.
-Removing of framing []
-Convert object to associative array

Thanks,
Ech

von ech madriaga - am 25.11.2015 13:24
Ech,

for WD20:

This will work fine with the variant. Use the power of the debugger to see how the variant is constructed to use it later on in your for each loop.

[attachment 1779 screen1.png]

You see that the JSON structure is converted to the Variant

If you use this code (Code from the 'convert' button)


sJsonData is string = EDT_NoName1

vMyData is Variant = JSONToVariant(sJsonData)

TableDeleteAll(TABLE_NoName1)
FOR EACH line OF vMyData
TableAddLine(TABLE_NoName1,line.modelName,line.type)
END


you will get this output:
[attachment 1780 screen2.png]

You will have to use the correct statements to get the dynamic data from the variant.

So it is very easy to work with JSON data.

Another way is to define a structure that represents your Json data and use deserialize to fill the structure from you JSON data, but this is more complex but it depends on the what you need the data for.

Hope this helps !
Danny

von Danny Lauwers - am 25.11.2015 14:17
Excellent!!
Thank you Danny Lauwers.

Now, I had done for displaying the information from json.
Can you help me on how I can add, modify, and delete a information using json?
Do you have any samples?

Thanks,
Ech

von ech madriaga - am 26.11.2015 09:14
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.