I have a string field with the following data:
{"vin":"VIN4","brand":"Merk4","type":"Type4","year":2004}
How can I retrieve the value for the fields vin, brand, type and year and print them in my document?
My string never contains repeating data.
I cannot use query to retrieve the data from the json string (I use Servoy foundset).
1 Answer:
Posted on August 11, 2021 at 12:51am
Here is some red hot garbage that might do the trick:
$V{Variable_1}.substring($V{Variable_1}.indexOf("\"vin\":") + new String("\"vin\":").length() + 1).substring(0,$V{Variable_1}.substring($V{Variable_1}.indexOf("\"vin\":") + new String("\"vin\":").length() + 1).indexOf("\",\""))
This should return the value for "vin" for data stored in $V{Variable_1}.
Yes. I know. This is disgusting. ;-)
Update 2022: I'm stupid and this is the proper way to do it:
new org.json.JSONObject($V{JSON}).get("vin").toString()
Thanks for you reply. It works perfectly. And I managed to make the code even more disgusting :-)
$V{Variable_1}.contains("vin")?
$V{Variable_1}.substring($V{Variable_1}.indexOf("\"vin\":") + new String("\"vin\":").length() + 1).substring(0,$V{Variable_1}.substring($V{Variable_1}.indexOf("\"vin\":") + new String("\"vin\":").length() + 1).indexOf("\",\"")):"no VIN specified"
If the vin field doest not exist, it will not show wrong info.