2014-09-11 126 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