我正在接收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
實例的位置。有沒有什麼公然錯誤的我如何嘗試這樣做,因爲我沒有得到太多的錯誤消息的幫助?
你可能想看看這個問題:http://stackoverflow.com/questions/3449099/parse-json-with-gwt-2-0/如果你信任你的源代碼,你應該使用JavaScript Overlay Types。通過JSONParser解析JSON是一個PITA(但是當源不受信任時必須:/)。 – 2010-08-16 08:26:07