[
{"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;iSize();i++)
{
TJSONPair* pair = jsonread0->Get(i);
At this time, pair.JsonValue is NULL. What do I need to do to read these values?
Try these samples
Delphi
{$APPTYPE CONSOLE}
uses< br /> 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
ParseJs on;
except
on E: Exception do
Writeln(E.ClassName,':', E.Message);
end;
Readln;
end.
C Builder
#include
#include
#pragma hdrstop
#pragma argsused
#include
#include
#include< br />#include
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{
TJSONValue* LJsonValue = LJsonArr->Get(i);
TJSONArray* LJsonArr2 = (TJSONArray*)LJsonValue;
int size2 = LJsonArr2->Size();
for (int j = 0; 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
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;iSize(); i++)
{
TJSONPair* pair = jsonread0->Get(i);At this time, pair.JsonValue is NULL. What do I need to do to read these values?
You did not convert the JSON string correctly, you must cast to TJSONArray and then iterate the elements.
Try these Sample
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"}'+< br />']';
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
< /p>
#include
#include
#pragma hdrstop
#pragma argsused
#include
#include
#include
#include
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{
TJSONValue* LJsonValue = LJsonArr->Get(i);
TJSONArray* LJsonArr2 = (TJSONArray*)LJsonValue;
int size2 = LJsonArr2->Size();
for (int j = 0; j{
TJSONValue * LItem = LJsonA rr2->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();< br /> return 0;
}This will return
EventType: 49
Code: 234
EventDate : 20050202
Result: 1
EventType: 48
Code: 0120
EventDate: 20130201
Group: g1< p>