How to parse JSON array in RAd Studio?

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 am using 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);

      

It pair.JsonValue

is NULL at this point . What do I need to do to read the values?

+3


source to share


4 answers


You are not producing the correct JSON String, you have to use it as a 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

      

+13


source


dbExpress JSON parser was considered cumbersome and sometimes problematic.



Maybe you can choose a number of third party parsers, for example this shows the read array: http://code.google.com/p/superobject/wiki/first_steps

+2


source


You have an invalid type, so you see undefined behavior. A null result is just one of many possible results that you might expect from this code. The function ParseJSONValue

in this case should return TJsonArray

, not TJsonObject

. Although both classes have methods Get

, they are not interchangeable.

The array method Get

returns a TJsonValue

, not TJsonPair

. For this specific data, you can enter a value TJsonObject

because your data is an array of two objects.

Use dynamic_cast

or Delphi as

to port from one class to another.

+1


source


you can get array from JSON string also using 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 for reference: JSONCBuilderBlog library.

+1


source







All Articles