How to parse a JSON array in RAd Studio?

2020-02-26 02:08发布

问题:

I am trying to parse the following Json document:

[
  {"EventType":49,"Code":"234","EventDate":"20050202", "Result":1},
  {"EventType":48,"Code":"0120","EventDate":"20130201", "Group":"g1"}
]

I use the following code:

TJSONObject* jsonread0 = (TJSONObject*) TJSONObject::ParseJSONValue(TEncoding::ASCII->GetBytes(Memo1->Lines->Text), 0);

for(int i=0;i<jsonread0->Size();i++)
{
    TJSONPair* pair = jsonread0->Get(i);

At this point, pair.JsonValueis NULL. What do I need to do to read the values?

回答1:

You are not casting the JSON String properly, you must cast as an TJSONArray and then iterate over the elements.

try these samples

Delphi

{$APPTYPE CONSOLE}

uses
  DBXJSON,
  System.SysUtils;

Const
StrJson =
  '['+
  '{"EventType":49,"Code":"234","EventDate":"20050202", "Result":1},'+
  '{"EventType":48,"Code":"0120","EventDate":"20130201", "Group":"g1"}'+
  ']';


procedure ParseJson;
var
  LJsonArr   : TJSONArray;
  LJsonValue : TJSONValue;
  LItem     : TJSONValue;
begin
   LJsonArr    := TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(StrJson),0) as TJSONArray;
   for LJsonValue in LJsonArr do
   begin
      for LItem in TJSONArray(LJsonValue) do
        Writeln(Format('%s : %s',[TJSONPair(LItem).JsonString.Value, TJSONPair(LItem).JsonValue.Value]));
     Writeln;
   end;
end;

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

C++ Builder

#include <vcl.h>
#include <windows.h>

#pragma hdrstop
#pragma argsused

#include <tchar.h>
#include <stdio.h>
#include <DBXJSON.hpp>
#include <iostream>


int _tmain(int argc, _TCHAR* argv[])
{
    TJSONArray* LJsonArr = (TJSONArray*)TJSONObject::ParseJSONValue(
    BytesOf((UnicodeString)"[{\"EventType\":49,\"Code\":\"234\",\"EventDate\":\"20050202\", \"Result\":1},  {\"EventType\":48,\"Code\":\"0120\",\"EventDate\":\"20130201\", \"Group\":\"g1\"}]"),0);
    int size = LJsonArr->Size();
    for (int i = 0; i < size; ++i)
    {
      TJSONValue* LJsonValue = LJsonArr->Get(i);
      TJSONArray*  LJsonArr2 =  (TJSONArray*)LJsonValue;
      int size2 = LJsonArr2->Size();
        for (int j = 0; j < size2; ++j)
        {
          TJSONValue* LItem   = LJsonArr2->Get(j);
          TJSONPair* LPair = (TJSONPair*)LItem;
          printf("%s %s \n", (UTF8String )(LPair->JsonString->Value()).c_str(),  (UTF8String )(LPair->JsonValue->Value()).c_str());
        }
    }
    std::cin.get();
    return 0;
}

This will return

EventType : 49
Code : 234
EventDate : 20050202
Result : 1

EventType : 48
Code : 0120
EventDate : 20130201
Group : g1


回答2:

dbExpress JSON parser was told to be heavyweight and sometimes problematic.

Perhaps you can choose some of the number of 3rd-party parsers, for example this shows reading array: http://code.google.com/p/superobject/wiki/first_steps



回答3:

You have an invalid type cast, so what you're seeing is undefined behavior. A null result is just one of the many possible outcomes you could expect from this code. The ParseJSONValue function in this case should return a TJsonArray, not a TJsonObject. Although both classes have Get methods, they're not interchangeable.

The array's Get method returns a TJsonValue, not a TJsonPair. For this particular data, you can type-cast the value to TJsonObject because your data represents an array of two objects.

Use dynamic_cast or Delphi's as operator to cast from one class to another.



回答4:

you can get an array from a JSON string also using the JSonCBuilderBlog Library for C++Builder (free and open source):

UnicodeString JSONSource =
"[{\"EventType\":49,\"Code\":\"234\",\"EventDate\":\"20050202\", \"Result\":1},"
"{\"EventType\":48,\"Code\":\"0120\",\"EventDate\":\"20130201\",\"Group\":\"g1\"}]";

int           Type; 
UnicodeString Code;
UnicodeString Date;
int           Result;

TMetaObject MyArray;

MyArray.Decode(JSONSource);

for(int i=0; i < MyArray.Count(); i++)
{
    Type   = MyArray[i]["EventType"];
    Code   = MyArray[i]["Code"];
    Date   = MyArray[i]["EventDate"];
}

The syntax is very simple, see the following link as reference: JSONCBuilderBlog library.