amit 3 Posted February 27, 2021 I try to retreive the data from JSON string that return from the server. Here is the JSON String {"RESULT":200, "IDLIST":[1,2,3,4,5]} I wrote delphi code as shown below to get the Array but it return empty string. var jso: TJsonObject; jsv: TJsonValue; s, RetResult, RetIDList: string; begin s := '{"RESULT":200, "IDLIST":[1,2,3,4,5]}'; jso := TJsonObject.ParseJSONValue(s) as TJsonObject; jsv := jso.GetValue('RESULT'); if jsv<>nil then RetResult := jsv.Value else RetResult := ''; // <-- this is OK. RetResult will be '200'. jsv := jso.GetValue('IDLIST'); if jsv<>nil then RetIDList := jsv.Value else RetIDList := ''; // <---I alway get the empty string for RetIDList. end; I try to use jsonpair instead with the code below. I will get the array string. var jso: TJsonObject; jsv: TJsonValue; jsp: TJsonpair; s, RetResult, RetIDList: string; begin s := '{"RESULT":200, "IDLIST":[1,2,3,4,5]}'; jso := TJsonObject.ParseJSONValue(s) as TJsonObject; jsv := jso.GetValue('RESULT'); if jsv<>nil then RetResult := jsv.Value else RetResult := ''; // <-- this is OK. RetResult will be '200'. for iJsonPair in jso do begin if (iJsonPair.JsonString.value = 'IDLIST') then begin RetIDList := iJsonPair.JsonValue.ToJson; // <-- This is OK. RetIDList wlll be '[1,2,3,4,5]'. // RetIDList := iJsonPair.JsonValue.Value; // <-- if I use this line instead I got empty string; break; end; end; end; Why? What is the different between iJsonPair.JsonValue.ToJson and iJsonPair.JsonValue.Value ? Share this post Link to post
Attila Kovacs 629 Posted February 27, 2021 IDLIST is an array of integer, why do you want to convert it to a string? Share this post Link to post
amit 3 Posted February 27, 2021 I want to use them to construct the SQL to update record flag in SQL table. The Bracket in the RetIDList will be romoved and finally the RetIDList will be '1,2,3,4,5'. Then I will create Update SQL as qry.SQL.clear; qry.SQL.add('Update Table1 Set Flag=0 where ID in ('+RetIDList+');'); qry.ExecSql; Anyway, if you can point me how to access IDLIST as an array of integer it will be OK. I can generate the SQL from that array. Share this post Link to post
Uwe Raabe 2057 Posted February 27, 2021 18 minutes ago, amit said: if you can point me how to access IDLIST as an array of integer it will be OK Declare a class like this and create an instance with TJson.JsonToObject from REST.Json.pas: type TMyJson = class FResult: Integer; FIdList: TArray<Integer>; end; const cJson = '{"RESULT":200, "IDLIST":[1,2,3,4,5]}'; var myJson: TMyJson; begin myJson := TJson.JsonToObject<TMyJson>(cJson); try for var I := 0 to Length(myJson.FIdList) - 1 do { do something with myJson.FIdList[I] } finally myJson.Free; end; end; 2 Share this post Link to post
Remy Lebeau 1394 Posted March 1, 2021 (edited) On 2/27/2021 at 2:44 AM, amit said: I wrote delphi code as shown below to get the Array but it return empty string. That is because IDLIST is an array (of integers), but you are trying to read it as a string. jso.GetValue('IDLIST') will return a TJSONValue pointer to a TJSONArray, which does not implement the Value() method, so it returns a blank string. You need to type-cast the TJSONValue to TJSONArray and enumerate its elements, eg: var jso: TJsonObject; jsa: TJSONArray; s, RetIDList: string; i: Integer; begin s := '{"RESULT":200, "IDLIST":[1,2,3,4,5]}'; jso := TJsonObject.ParseJSONValue(s) as TJsonObject; if jso <> nil then try ... RetIDList := ''; jsa := jso.GetValue('IDLIST') as TJSONArray; if jsa <> nil then begin if jsa.Count > 0 then begin RetIDList := jsa[0].Value; for i := 1 to jsa.Count-1 do RetIDList := RetIDList + ',' + jsa[i].Value; end; end; ... finally jso.Free; end; end; Edited March 1, 2021 by Remy Lebeau 1 Share this post Link to post
Howells 1 Posted March 5, 2021 (edited) var jsonString = '{"x": {"y": [2,3]}, "t": [3,5]}'; var obj = JSON.parse(jsonString); var arr = []; arr.push(obj.x.y) arr.push(obj.t) console.log(arr); I was wondering the same thing when I was working on https://www.slotozilla.com/free-slots/silver-lion .Don't create your json yourself, most languages have functions to turn your object/array into a valid json, so just create your object as you normally do and then use that function as in Javascript JSON.stringify(). Edited March 5, 2021 by Howells misspelled the point 1 Share this post Link to post
aehimself 396 Posted March 5, 2021 On 2/27/2021 at 12:59 PM, amit said: I want to use them to construct the SQL to update record flag in SQL table. The Bracket in the RetIDList will be romoved and finally the RetIDList will be '1,2,3,4,5'. Then I will create Update SQL as qry.SQL.clear; qry.SQL.add('Update Table1 Set Flag=0 where ID in ('+RetIDList+');'); qry.ExecSql; Just two notices. If you will ever use Oracle and RetIDList will contain more than 1000 elements, the code will fail. I don't know if any other RDBMS has this limitation though. How trusted is the file? Taking a string value from somewhere and putting it in a SQL command exposes your application to injection attacks. 2 Share this post Link to post
amit 3 Posted March 6, 2021 (edited) 22 hours ago, aehimself said: Just two notices. If you will ever use Oracle and RetIDList will contain more than 1000 elements, the code will fail. I don't know if any other RDBMS has this limitation though. How trusted is the file? Taking a string value from somewhere and putting it in a SQL command exposes your application to injection attacks. I limit the data send to server at 25 records at a time. Anyway I wonder how I can make sure that the server can successfully insert the whole data on the database without returning the successfully inserted ID. if I just check only the http status 200 returned, I knew only that the server successfully received the whole json string but it is not guarantee that the whole data are inserted successfully on the database. In my server code, I return just only the successfully inserted record IDs, the problem ones will not be returned. The client will update the sentflag on any records that ID are in the RetIDList. So on the next data send phase, the problem ones can be sent again. Is there any other way to check whether there is no problem at all on the server side? Please suggest. Thank you. Oh another thing the IDs that send to the server in Json string is the ID of the records in database's table in the client PC and it will never be inserted to the Server's database for any reference. They will be used for server to reply back to the client if they are inserted succesfully. Client will mark those records by setting sentflag so no need to resend them. Edited March 6, 2021 by amit add some clarify message Share this post Link to post
aehimself 396 Posted March 6, 2021 (edited) 8 hours ago, amit said: Is there any other way to check whether there is no problem at all on the server side? Please suggest. Thank you. Well, I have 2 ideas. 1 - use the HTTP status codes. 200 means all fine, it was inserted. 500 means an error happened, transaction was rolled back, resend is needed from the client. If you can not control the status codes, you can add a "status check API". After a 200 OK for inserting the record, the client can query the inserted IDs for verification. If I understand the question completely, that is. But, I never really worked with stuff like this so take this with a grain of salt. I always liked to control everything, so when it was needed I launched my own webserver via ICS. That way I could set return codes, headers, session cookies, and even send detailed answers in any format whenever I wanted to. Edited March 6, 2021 by aehimself Share this post Link to post
amit 3 Posted April 2, 2021 Sample code for someone who need to work on JSON. JsonDemo.rar Share this post Link to post
nullcode 0 Posted June 11, 2021 On 4/2/2021 at 4:13 AM, amit said: Sample code for someone who need to work on JSON. JsonDemo.rar Damaged file Share this post Link to post
jamecorbitt 0 Posted April 28, 2022 Try next one method: public class Test { public static void main(String[] args) throws JSONException { String data = "[{\"userName\": \"sandeep\",\"age\":30},{\"userName\": \"vivan\",\"age\":5}] "; JSONArray jsonArr = new JSONArray(data); for (int i = 0; i < jsonArr.length(); i++) { JSONObject jsonObj = jsonArr.getJSONObject(i); System.out.println(jsonObj); } } } This way I used and it coincides with the opinion of people on Stackoverflow. Check next source: https://stackoverflow.com/questions/15609306/convert-string-to-json-array/source Regards, Jame Corbitt Share this post Link to post
David Heffernan 2345 Posted April 28, 2022 (edited) 9 hours ago, jamecorbitt said: Try next one method: public class Test { public static void main(String[] args) throws JSONException { String data = "[{\"userName\": \"sandeep\",\"age\":30},{\"userName\": \"vivan\",\"age\":5}] "; JSONArray jsonArr = new JSONArray(data); for (int i = 0; i < jsonArr.length(); i++) { JSONObject jsonObj = jsonArr.getJSONObject(i); System.out.println(jsonObj); } } } This way I used and it coincides with the opinion of people on Stackoverflow. Check next source: https://stackoverflow.com/questions/15609306/convert-string-to-json-array/source Regards, Jame Corbitt Quite odd to copy some code from an SO question that is in the wrong language. This code is Java. Edited April 28, 2022 by David Heffernan Share this post Link to post
Lars Fosdal 1792 Posted May 1, 2022 Another method to do JSON to Object. unit StructureTestTypes; interface uses REST.Json, System.SysUtils; type TStructure = class(TObject) // '{"RESULT":200, "IDLIST":[1,2,3,4,5]}' private FIDLIST: TArray<Integer>; FRESULT: integer; public constructor Create; virtual; destructor Destroy; override; property RESULT: integer read FRESULT write FRESULT; property IDLIST: TArray<Integer> read FIDLIST write FIDList; end; implementation { TStructure } constructor TStructure.Create; begin SetLength(FIDLIST, 0); end; destructor TStructure.Destroy; begin SetLength(FIDLIST, 0); inherited; end; end. Test Program: program JsonStructureTest; {$APPTYPE CONSOLE} {$R *.res} uses System.SysUtils, REST.Json, StructureTestTypes in 'StructureTestTypes.pas'; var Test: TStructure; begin Test := TJson.JsonToObject<TStructure>('{"RESULT":200, "IDLIST":[1,2,3,4,5]}']); try try Writeln('Result: ', Test.RESULT); Write('IDList:'); for var i in Test.IDLIST do Write(' ', i ); Writeln; except on E: Exception do Writeln(E.ClassName, ': ', E.Message); end; finally Test.Free; Writeln; Write('Press Enter: '); Readln; end; end. 1 Share this post Link to post
Edd 0 Posted September 6 On 3/5/2021 at 10:15 AM, Howells said: var jsonString = '{"x": {"y": [2,3]}, "t": [3,5]}'; var obj = JSON.parse(jsonString); var arr = []; arr.push(obj.x.y) arr.push(obj.t) console.log(arr); I was wondering the same thing when I was working on https://www.slotozilla.com/free-slots/silver-lion site. Don't create your json yourself, most languages have functions to turn your object/array into a valid json, so just create your object as you normally do and then use that function as in Javascript JSON.stringify(). You're right, manually creating JSON strings can lead to mistakes. It's much easier to use built-in functions like JSON.stringify() in JavaScript to convert objects/arrays into valid JSON. This ensures your JSON is always valid without worrying about formatting errors. If you're working with objects frequently, it’s best to rely on these functions rather than manually constructing JSON strings. Share this post Link to post