2010-08-16 194 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