2010-08-16 181 views
9

我正在接收JSON字符串的GWT應用程序,並且我很難找到每個對象的值。我試圖將傳入的JSON字符串轉換爲對象數組。GWT:處理傳入的JSON字符串

這裏是JSON(從Firebug的響應選項卡)中, 「d」 是一個.NET的事情(Web服務正在消耗的C#。

{ 
    "d": [ 
     { 
      "__type": "Event", 
      "ID": 30, 
      "Bin": 1, 
      "Date": "\/Date(1281544749000)\/", 
      "Desc": "Blue with white stripes.", 
      "Category": "1" 
     }, 
     { 
      "__type": "Event", 
      "ID": 16, 
      "Bin": 3, 
      "Date": "\/Date(1281636239000)\/", 
      "Desc": "Yellow with pink stripes", 
      "Category": "1" 
     } 

    ] 
} 

我想給JSON解析成的對象,然後將它們插入到一個數組,我能夠使用Window.alert並得到整個「d」對象呼應。然而,當我嘗試訪問數組中的元素,GWT調試器只是崩潰。

//My GWT array to receive JSON Array 
ArrayList<Item> itemInfo = new ArrayList<Item>(); 

//Getting response JSON into something I can work with.(THIS FAILS) 
JSONArray jsonValue = JSONParser.parse(incomingJsonRespone); 

//Just trying to verify I'm getting values 
for (int i=0; i<jsonValue.size(); i++) { 
    JSONValue jsonItem = = JsonValue.get(i).getString(); 
    Window.alert(jsonItem); 
    itemInfo.add(jsonItem); 

}

我想我已經將問題縮小到了創建JSONArray實例的位置。有沒有什麼公然錯誤的我如何嘗試這樣做,因爲我沒有得到太多的錯誤消息的幫助?

+1

你可能想看看這個問題:http://stackoverflow.com/questions/3449099/parse-json-with-gwt-2-0/如果你信任你的源代碼,你應該使用JavaScript Overlay Types。通過JSONParser解析JSON是一個PITA(但是當源不受信任時必須:/)。 – 2010-08-16 08:26:07

回答

16

針對RMorrisey的評論:
其實,它更令人費解:/它看起來是這樣的(未測試的代碼,但你應該得到的總體思路):

JSONValue jsonValue; 
JSONArray jsonArray; 
JSONObject jsonObject; 
JSONString jsonString; 
jsonValue = JSONParser.parseStrict(incomingJsonRespone); 
// parseStrict is available in GWT >=2.1 
// But without it, GWT is just internally calling eval() 
// which is strongly discouraged for untrusted sources 

if ((jsonObject = jsonValue.isObject()) == null) { 
    Window.alert("Error parsing the JSON"); 
    // Possibilites: error during download, 
    // someone trying to break the application, etc. 
} 

jsonValue = jsonObject.get("d"); // Actually, this needs 
           // a null check too 
if ((jsonArray = jsonValue.isArray()) == null) { 
    Window.alert("Error parsing the JSON"); 
} 

jsonValue = jsonArray.get(0); 
if ((jsonObject = jsonValue.isObject()) == null) { 
    Window.alert("Error parsing the JSON"); 
} 

jsonValue = jsonObject.get("Desc"); 
if ((jsonString = jsonValue.isString()) == null) { 
    Window.alert("Error parsing the JSON"); 
} 

Window.alert(jsonString.stringValue()); // Finally! 

正如你所看到的,當使用JSONParser時,您必須/應該非常謹慎 - 這就是整個觀點,對吧?解析不安全的JSON(否則,就像我在評論中所建議的那樣,您應該使用JavaScript Overlay Types)。你得到一個JSONValue,檢查它是否真的應該是你認爲的,比如JSONObject,你得到JSONObject,檢查它是否有「xyz」鍵,你得到JSONValue,沖洗並重復。這不是最有趣的工作,但至少它的安全不僅僅是對整個JSON :)
注意提醒eval()賈森指出,之前GWT 2.1,JSONParser使用eval()內(它只有一個parse()方法 - GWT 2.0 javadocs與GWT 2.1)。在GWT 2.1中,parse()已被棄用,並且引入了另外兩種方法 - parseLenient()(內部使用eval())和parseStrict()(安全方法)。如果你真的需要使用JSONParser,那麼我建議升級到GWT 2.1 M2,否則你可能會使用JSO。作爲不受信任來源的JSONParser的替代方法,您可以嘗試整合json2.js as a JSON parser via JSNI

PS:cinqoTimo,JSONArray jsonValue = JSONParser.parse(incomingJsonRespone);顯然是行不通的,因爲JSONParser.parseJSONValue返回類型,不JSONArray - 沒有你的IDE(Eclipse的谷歌+插件?)警告你?或者至少是編譯器。

+1

確保爲不可信/任意的JSON使用'JSONParser'' parseStrict()'方法:」注意!爲了提高效率,該方法使用JavaScript'eval()'函數實現,該函數可以執行任意腳本。不要將不可信的字符串傳遞給這個方法。「 (來自http://code.google.com/p/google-web-toolkit/source/browse/trunk/user/src/com/google/gwt/json/client/JSONParser.java) – 2010-08-16 13:47:40

+0

嗯,我在「JSONParser」使用更安全的方法的印象......我會建議'parseStrict()',但後來我注意到它已經在GWT 2.1中引入,所以OP可能還沒有它。但是根據Jason的評論,我認爲最好升級到GWT 2.1 M2並使用'parseStrict()',否則'JSONParser'對JSO幾乎沒有任何好處。 – 2010-08-16 14:25:44

+0

代碼不完全正確,但這個想法是正確的。感謝 – tpow 2010-08-16 17:43:49

2

看起來您沒有數組,但是隻有一個根對象,其名稱爲'd'的屬性是一個數組。我不熟悉那個特定的API,但也許你可以嘗試檢索一個JSONObject或類似的而不是數組?

+0

謝謝,我想我明白你在說什麼。你的意思是把它解析成JSONObject,然後解析成JSONArray?或者直接從JSONObject訪問字符串值? – tpow 2010-08-16 02:47:52

+0

您是否有指向正在使用的JSONParser庫的API文檔的鏈接? – RMorrisey 2010-08-16 03:39:02

+1

我不知道什麼是正確的調用,但我認爲它應該是這樣的:JSONParser.parse(incomingJsonResponse).getObject(「d」)。getArray() 或JSONParser.parse(incomingJsonResponse).getArray(「 d「) – RMorrisey 2010-08-16 03:39:57