PC SOFT

FORUMS PROFESSIONNELS
WINDEVWEBDEV et WINDEV Mobile

Accueil → WINDEV 2024 → Parsing Json
Parsing Json
Débuté par steve erts, 15 oct. 2017 02:07 - 4 réponses
Posté le 15 octobre 2017 - 02:07
Hi,

I'm trying to get a handle on how to parse a JSON data payload. I'm able to use JSONToVariant to convert payload to JSON and then VariantToJSON to convert back into JSON and this seems to confirm that the payload stays in tact but I'm having problems figuring out how to deal with a JSON structure that is multi -tiered

Below is an example file. How would I navigate down to the "itinerary_days" section and then loop through the individual days?

There seems to be a lot more capabilities in WD for moving around an XML file than there is for JSON but maybe I'm missing something?

{
"id":540,
"title":"14-Day Ultimate Namibia Safari",

"itinerary_days": [

{
"DayNbr":1,
"Title":"Day 1"
},{
"DayNbr":2,
"Title":"Day 2"
},{
"DayNbr":3,
"Title":"Day 3"
},{
"DayNbr":4,
"Title":"Day 4"
},{
"DayNbr":5,
"Title":"Day 5"
},{
"DayNbr":6,
"Title":"Day 6"
},{
"DayNbr":7,
"Title":"Day 7"
},{
"DayNbr":8,
"Title":"Day 8"
},{
"DayNbr":9,
"Title":"Day 9"
},{
"DayNbr":10,
"Title":"Day 10"
},{
"DayNbr":11,
"Title":"Day 11"
},{
"DayNbr":12,
"Title":"Day 12"
},{
"DayNbr":13,
"Title":"Day 13"
},{
"DayNbr":14,
"Title":"Day 14"
}
]
}
Posté le 15 octobre 2017 - 13:41
Hi Steve,

If I remember correctly, you just do a for each loop inside a for each loop for each sublevel.

best regards
Posté le 15 octobre 2017 - 20:04
Hi Fabrice,

I've gotten far enough along to see that there are 14 elements within the variant member "itinerary_days" but I'm not understanding how to run a for each on that substring section.

Here is my code.


sText is ANSI string=fLoadText("c:\ emp\jsontest.json.good.txt")

vItin is Variant=JSONToVariant(sText)

FOR EACH x OF vItin..Member


IF x..Name="itinerary_days" THEN


Info(x..Occurrence)


// I'm guessing I'd need to do some sort of for each loop but I'm stuck figuring out what that looks like.



END


END
Posté le 15 octobre 2017 - 21:02
Hi Steve,
here is solution
sText is ANSI string=fLoadText("c:\ emp\jsontest.json.good.txt") vItin is Variant=JSONToVariant(sText) FOR i=1 _TO_ vItin.itinerary_days..Occurrence Trace(vItin.itinerary_days[ i ].DayNbr) Trace(vItin.itinerary_days[ i ].Title) END
BR,
Alen
Posté le 15 octobre 2017 - 21:29
Quote
Alen U.

Hi Steve,

here is solution


sText is ANSI string=fLoadText("c:\ emp\jsontest.json.good.txt")

vItin is Variant=JSONToVariant(sText)

FOR i=1 _TO_ vItin.itinerary_days..Occurrence
Trace(vItin.itinerary_days[ i ].DayNbr)
Trace(vItin.itinerary_days[ i ].Title)
END


BR,

Alen

Thanks!