During the development of DBXJSON in DelphiXE, I ran into a couple of problems

An example of a JSON response that we will parse:

{"num1":631, "num2":31, "num3":64, "data":"{ \"dat1\":[0,1,2,3,4], \"dat2\":null, \"dat3\":true, \"dat4\":[1,1,1,1,1], \"dat5\":{ \"dat4str1\":11, \"dat4str2\":0, \"dat4str3\":0, \"dat4str4\":2 }, \"sdata\":{ \"sdata1\":12, \"sdata2\":48, \"sdata3\":395, }, \"dat5\":[[0,1,1],[0,1,1],[0,1,1]] }" } 

1 . If the answer is in JSONText, then we get the whole object:

 JSONObj:=TJSONObject.ParseJSONValue(JSONText) As TJSONObject; 

Then select the data in a separate:

 JSONData:=TJSONObject.ParseJSONValue(JSONObj.Get('data').JsonValue.Value) As TJSONObject; 

And then you also need to select dat5, but the line:

 JSONData4:=TJSONObject.ParseJSONValue(JSONData.Get('dat5').JsonValue.Value) As TJSONObject; 

will not work, because of the slash in the name \ "dat5 \"

How to be? From php kstati everything parsitsya with a bang.

2 How to read null and true values?

  • I solved the problem as follows: JSONData: = TJSONObject.ParseJSONValue (TJSONObject.ParseJSONValue (JSONObj.Get ('data'). JsonValue.Value) .ToString) As TJSONObject; so we represent that in data as an object, and then we usually work with it usually JSONData5: = TJSONObject.ParseJSONValue (JSONData.Get ('dat5'). JsonValue.ToString) As TJSONObject; dat4str2: = StrToInt (JSONData5.Get ('dat4str2'). JsonValue.ToString); And for boolean values ​​Dat3: = StrToBool (JSONData.Get ('dat3'). JsonValue.ToString); - Isaev

1 answer 1

In general, it is strange that "decorated as \". Both 'and' are normally perceived by JavaScript. And in JSON these \ in general should not be.

  1. Replace \ "on".
  2. I do not know about DBXJSON, I do not use it, but isp. superobject would be like this: <code> var Json: ISuperObject; begin
    json: = SO ('JSON Text');
    if json.N ['data.dat2']. IsType (stNull) then
    //Data.Dat2 = NULL

if json.B ['data.dat3'] then //Data.Dat3 = TRUE end; </ code>

  • Thanks for the answer ... Evaluation is not yet allowed - Isaev