2014-09-11 52 views
1

我下载了XSuperObject从Web服务器读取Json,但是在将Json字符串添加到ISuperArray的时刻,我收到了Segmentation错误。XSuperObject分段错误11 Delphi XE6

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; 

下面的代码工作,但它需要2秒读取其中有17场的每个记录并有800我使Eclipse的应用程序需要10秒,所有800

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

您确定要接收阵列'[...]',而不是一个对象'{...}'?请添加收到的JSON字符串的示例。 – 2014-09-11 07:37:31

+0

如果没有输入数据,问题是不完整的。请重新访问该问题,并将其转换为硬编码输入数据的简短控制台应用程序。 – 2014-09-11 07:52:01

+0

谢谢。我无法在Windows上重现。但我想你正在运行在不同的平台上。哪个平台? – 2014-09-12 08:06:41

回答

2

您提供的节目,你没有得到一个JSON阵列([data, data, data])样品JSON,而是一个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-超对象样本这里:https://code.google.com/p/x-superobject/wiki/Sample