我下载了XSuperObject,用于从Web服务器读取Json,但是在我将Json字符串添加到ISuperArray时,我收到了Segmentation错误。
JsonResult : string;
JsonResult := IdHTTP1.Get('http://.................');
LoadJSONXSuperObject(JsonResult);
procedure TDataForm.LoadJSONXSuperObject(S: String);
var
aobj: ISuperArray;
obj2: ISuperObject;
I: Integer;
MyString: String;
begin
aobj := SA(S); // RIGHT HERE I GET THE fault (11) or bus (10)
for I := 0 to aobj.Length-1 do
begin
end;
以下代码有效,但读取每个有17个字段的记录需要2秒钟,有800个我在Eclipse中制作相同的应用程序,所有800个需要10秒。
try
LResult := LJsonObj.Get('d').JsonValue as TJsonObject;
LElements := LResult.Get('results').JsonValue as TJsonArray;
for i := 0 to LElements.count -1 do
begin
Try
LItem := (LElements.Get(i) as TJsonObject).Get('pbutton').JsonValue as TJsonString;
if LItem <> nil then
PButton := RemoveQuotes(LItem.ToString)
else PButton := '';
except
PButton := '';
End;
Try
LItem := (LElements.Get(i) as TJsonObject).Get('text').JsonValue as TJsonString;
if LItem <> nil then
InvText := RemoveQuotes(LItem.ToString)
else InvText := '';
except
InvText := '';
End;
Try
LItem := (LElements.Get(i) as TJsonObject).Get('buttontext').JsonValue as TJsonString;
if LItem <> nil then
ButtonText := RemoveQuotes(LItem.ToString)
else ButtonText := '';
except
ButtonText := '';
End;
end;
finally
end;
以下是Json文件的示例。
{
"d": {
"results": [
{
"__metadata": {
"uri": "http://myserver",
"key_fields": "",
"rows_affected": -1,
"last_autoinc": 0
},
"pbutton": 1,
"text": "Pizza",
"buttontext": "Pizza",
"price1": 10.99
},
{
"__metadata": {
"uri": "http://myserver",
"key_fields": "",
"rows_affected": -1,
"last_autoinc": 0
},
"pbutton": 2,
"text": "Pizza 2",
"buttontext": "Pizza 2",
"price1": 10.99
},
{
"__metadata": {
"uri": "http://myserver",
"key_fields": "",
"rows_affected": -1,
"last_autoinc": 0
},
"pbutton": 98,
"text": null,
"buttontext": null,
"price1": 0
}
]
}
}
答案 0 :(得分:2)
您提供的示例json表明您没有获得json数组([data, data, data]
),而是获得json对象({data}
)。您必须使用SO(S)
代替SA(S)
。
uses
XSuperObject,
XSuperJSON;
procedure TDataForm.LoadJSONXSuperObject(const S: string);
var
jsonObj, currentObj: ISuperObject;
enum: TSuperEnumerator<IJSONAncestor>;
begin
jsonObj:= SO(S);
enum := jsonObj['d.results'].AsArray.GetEnumerator;
while enum.MoveNext do
begin
currentObj := enum.Current.AsObject;
PButton := currentObj.I['pbutton'];
InvText := currentObj.S['text'];
ButtonText := currentObj.S['buttontext'];
// Price := currentObj.F['price1'];
end;
end;
另请参阅此处的X-SuperObject示例:https://code.google.com/p/x-superobject/wiki/Sample