Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse a json string response using Delphi

I have a rest server returning the next json string:

response:='{"result":["[{\"email\":\"[email protected]\",\"regid\":\"12312312312312312313213w\"},{\"email\":\"[email protected]\",\"regid\":\"AAAAAAA\"}]"]}';

I´d like to parse the response to get a list of all email and regid items.

I have tried the next code but I am getting an AV at (TJSONPair(LItem).JsonString.Value='email')

Any help will be appreciated.

Thanks in advance, Luiz

var
  LResult:TJSONArray;
  LJsonresponse:TJSONObject;
  i:integer;
  LItem,jv:TJsonValue;
  email,regid:string;

      LJsonresponse:=TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(response),0) as TJSONObject;
      LResult:=(LJsonresponse.GetValue('result') as TJSONArray);
      jv:=TJSONArray(LResult.Get(0));
      for LItem in TJSONArray(jv) do begin
         if (TJSONPair(LItem).JsonString.Value='email') then begin
           email:=TJSONPair(LItem).JsonValue.Value;
         end;
         if (TJSONPair(LItem).JsonString.Value='regid') then begin
           regid:=TJSONPair(LItem).JsonValue.Value;
         end;
      end;
like image 916
Luiz Alves Avatar asked Jul 13 '15 18:07

Luiz Alves


People also ask

How do I read a JSON file in Delphi?

ASCII. GetBytes(TFile. ReadAllText(filename)), 0); if JSONValue is TJSONArray then begin for jv in (JSONValue as TJSONArray) do begin if jv is TJSONObject then begin jo := jv as TJSONObject; for pair in jo do begin Append(result, jo.

What is Jsonpath parse?

JSON parsing is the process of converting a JSON object in text format to a Javascript object that can be used inside a program. In Javascript, the standard way to do this is by using the method JSON.

How do I query a JSON file in SQL?

To query JSON data, you can use standard T-SQL. If you must create a query or report on JSON data, you can easily convert JSON data to rows and columns by calling the OPENJSON rowset function. For more information, see Convert JSON Data to Rows and Columns with OPENJSON (SQL Server).

Is JSON a framework?

While JSON provides a syntactic framework for data interchange, unambiguous data interchange also requires agreement between producer and consumer on the semantics of specific use of the JSON syntax.


1 Answers

Your problems start here:

jv := TJSONArray(LResult.Get(0));

The problem is that LResult.Get(0) does not return an instance of TJSONArray. In fact it returns an instance of TJSONString. That string has value:

'[{"email":"[email protected]","regid":"12312312312312312313213w"},{"email":"[email protected]","regid":"AAAAAAA"}]'

It looks like you are going to need to parse this string as JSON to extract what you need. Here is some gnarly code that does that. Please excuse its quality because I have no experience at all with the Delphi JSON parser.

{$APPTYPE CONSOLE}

uses
  SysUtils, JSON;

const
  response =
    '{"result":["[{\"email\":\"[email protected]\",\"regid\":\"12312312312312312313213w\"},'+
    '{\"email\":\"[email protected]\",\"regid\":\"AAAAAAA\"}]"]}';

procedure Main;
var
  LResult: TJSONArray;
  LJsonResponse: TJSONObject;
  ja: TJSONArray;
  jv: TJSONValue;
begin
  LJsonResponse := TJSONObject.ParseJSONValue(response) as TJSONObject;
  LResult := LJsonResponse.GetValue('result') as TJSONArray;
  ja := TJSONObject.ParseJSONValue(LResult.Items[0].Value) as TJSONArray;
  for jv in ja do begin
    Writeln(jv.GetValue<string>('email'));
    Writeln(jv.GetValue<string>('regid'));
  end;
end;

begin
  try
    Main;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
  Readln;
end.

The big lesson here is to stop using unchecked type casts. Using such casts is asking for trouble. When your data does not match your code, you get unhelpful error messages.

like image 109
David Heffernan Avatar answered Sep 19 '22 03:09

David Heffernan