Author | Topic: JSON2Var | |
---|---|---|
Adam Mester | JSON2Var on Fri, 11 Aug 2017 14:53:53 +0200 Hello, I want to ask if it is possible with Xbase++ 2.0 (Build 2.00.817) to convert a JSON string to a DataObject and how? I see there is the reverse function Var2JSON() and it works as expected. The existence of this function is mentioned only on the "Home page" of the documentation, but nowhere else in details. On the "Home page" it says: "Use Var2JSON() and jQuery.parseJSON(data); in your mobile or web app to transform your Xbase++ objects into Javascript objects." I see that in the Workbench I can add the jQuery as an asset to my project but how can I use jQuery.parseJSON(data) in my project - or any other method that converts JSON to DataObject. Thanks in advance. | |
Peter Nagy | Re: JSON2Var on Wed, 18 Sep 2019 12:53:14 +0200 Hello "or any other method that converts JSON to DataObject." You can use my class that is convert json to DataObject. In the qsjson.prg there is a function Sample(), which is read the sample.json. Sample.json: {"olvasatlanUzenetekSzama":0,"uzenetekSzama":1,"uzenetek": [{"erkeztetesiSzam":"518124715201909031142542520","cimkek": [],"erkezesiDatum":1567503750000,"feladoNev":"Nemzeti AdĂł- Ă©s Vámhivatal","feladoRovidNev":"NAV","cimzettNev":"DR. SIMKĂ“ CSABA","cimzettRovidNev":"DR. SIMKĂ“ CSABA","fajlMeret":4289,"dokumentumTipus":"ElfogadĂł nyugta","dokumentumLeiras":"1908, Elfogadott bizonylat nyugta (18414020), 2019.08.01-2019.08.31, \"ErzsĂ©bet Hospice AlapĂtvány \"","hozzaferesEngedelyezett":true,"olvasatlan":false,"tarolasiHely":"BEER KEZETT"}]} Sample function: FUNCTION Sample() local loJson, loItems local lsStr loJson := QsJson():new( memoread("C:\MyApp\sample.json") ) loItems := loJson:GetTree() for i:=1 to len(loItems:uzenetek) //uzenetek is inside the json lcDocLeiras := loItems:uzenetek[i]:dokumentumLeiras //dokumentumLeiras is inside the json's uzenetek array MsgBox("doc description: " + lcDocLeiras); next RETURN nil So you can refer to any json as you expected, can use arrays also. But it is far from perfect (error text and some variables is in hungarian language), known issues: -If your json starts with array like: [{}], you just need to wrap the array in some variable, like: {thisisthearray:[{}]}, and now it works, you just need to go inside the variable, like thisisthearray[1]:something -If string has ' or " characters, that will not work. So you need to do something like: cStr := StrTran(cStr, "\'") cStr := StrTran(cStr, '\"') to remove the ' or " characters (which is encoded \' and \" inside the string). Maybe this is helping you. qsjson.prg sample.json | |
Peter Alderliesten | Re: JSON2Var on Thu, 19 Sep 2019 10:15:14 +0200 Adam, > I want to ask if it is possible with Xbase++ 2.0 (Build 2.00.817) to convert a > JSON string to a DataObject and how? > > I see there is the reverse function Var2JSON() and it works as expected. The > existence of this function is mentioned only on the "Home page" of the > documentation, but nowhere else in details. Both functions are documented in the Help-file: Var2json() and Json2var() The support an Array and a Dataobject als primary input They also have two accompanying functions: JsonEncode() and JsonDecode() for encoding and decoding character strings. Peter |