Diese Seite mit anderen teilen ...

Informationen zum Thema:
Forum:
WinDev Forum
Beiträge im Thema:
9
Erster Beitrag:
vor 2 Jahren, 4 Monaten
Letzter Beitrag:
vor 2 Jahren, 4 Monaten
Beteiligte Autoren:
Sammy Broeders, kingdr, MichaelG

Extract JSON string

Startbeitrag von Sammy Broeders am 16.10.2015 19:10

Hello,

I think a simple question, how can i extract a string what i receive from a webservice in JSON format? By example the received JSON text below, i want the "aantal_cilinders" in a string. By xml format it is very easy with XMLExtractString.

[{"aantal_cilinders":"5","aantal_deuren":"4","aantal_wielen":"4","aantal_zitplaatsen":"5","afstand_hart_koppeling_tot_achterzijde_voertuig":"0","afstand_voorzijde_voertuig_tot_hart_koppeling":"0","

Thanks!

Antworten:

JSONToVariant and VariantToJSON would be my advice.

von MichaelG - am 16.10.2015 20:56
I have tryed this a lot but it stil not works for me. In the Windev help i can`t find some more information.

von Sammy Broeders - am 17.10.2015 06:53
Or is there a way to convert the JSON data to XML data?

Thanks,
Sammy

von Sammy Broeders - am 17.10.2015 16:02
Hi

Pls see below example:

sJZon is ANSI string =[
{"aantal_cilinders":"5","aantal_deuren":"4","aantal_wielen":"4"}
]
jz is Variant
jz=JSONToVariant(sJZon)
trace("Results:", jz.aantal_cilinders, jz.aantal_wielen)

// Results: 5 4
// ok

HTH

Cheers

Galon

von kingdr - am 17.10.2015 19:58
If I were you, I'd do this as below:

sJZon is ANSI string =[
{"aantal_cilinders":"5",
"aantal_deuren":"4",
"aantal_wielen":"4",
"aantal_zitplaatsen":"5",
"afstand_hart_koppeling_tot_achterzijde_voertuig":"0",
"afstand_voorzijde_voertuig_tot_hart_koppeling":"0"
}
]
jz is Variant
jz=JSONToVariant(sJZon)
trace("Results:", jz.aantal_cilinders, jz.aantal_wielen, jz.afstand_voorzijde_voertuig_tot_hart_koppeling)

//output
// Results: 5 4 0

Cheers

King

von kingdr - am 17.10.2015 20:05
Thanks for the support,

I do as below and receive the error The variant is not an object.

ResStart is boolean

_url is string = "https://opendata.rdw.nl/resource/m9d7-ebf2.json?kenteken=66JHVB";

ResStart = HTTPRequest(_url)

sJZon is ANSI string = HTTPGetResult()
jz is Variant
jz=JSONToVariant(sJZon)
Info(jz.aantal_cilinders)


von Sammy Broeders - am 18.10.2015 11:00
Case closed, pls see as below:


ResStart is boolean
_url is string = "https://opendata.rdw.nl/resource/m9d7-ebf2.json?kenteken=66JHVB";
ResStart = HTTPRequest(_url)
sJZon is ANSI string = HTTPGetResult()
jz is Variant


sJZon = replace(sJZon,"[", "")
sJZon = replace(sJZon,"]", "")
sJZon = replace(sJZon,Charact(13), "")

jz=JSONToVariant(sJZon)
Info(jz.aantal_cilinders)
info(jz.api_gekentekende_voertuigen_brandstof)
//trace(jz.api_gekentekende_voertuigen_brandstof)

HTH


Cheers

King

von kingdr - am 18.10.2015 13:10
Thanks King!

Works ok for me.

von Sammy Broeders - am 18.10.2015 13:41
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.