GWT:处理传入的 JSON 字符串 [英] GWT: Dealing with incoming JSON string

查看:19
本文介绍了GWT:处理传入的 JSON 字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个接收 JSON 字符串的 GWT 应用程序,但我很难深入了解每个对象的值.我正在尝试将传入的 JSON 字符串传输到对象数组中.

I am working on a GWT app that is receiving a JSON string, and I'm having a hard time getting down to the values of each object. I'm trying to transfer the incoming JSON string into an array of objects.

这是 JSON(来自 Firebug 响应选项卡),d"是 .NET 的东西(正在使用的 Web 服务是 C#.

Here is the JSON (from Firebug response tab), The "d" is a .NET thing (Web Service Being Consumed is 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 调试器崩溃了.

I'm trying to parse the JSON into objects, and then insert them into an array. I'm able to use Window.alert and get the entire "d" object to echo. However, when I try to access the elements of the array, GWT debugger just crashes.

//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 实例的位置.我尝试这样做的方式是否存在明显错误,因为我在错误消息方面没有得到太多帮助?

I think I have narrowed down the problem to where the JSONArray instance is being created. Is there something blatantly wrong with how I'm trying to do this, because I'm not getting much help in the way of error messages?

推荐答案

回复 RMorrisey 的评论:
实际上,它更复杂:/它看起来像这样(代码未经测试,但您应该了解总体思路):

In response to RMorrisey's comment:
Actually, it's more convoluted :/ It would look something like this (code untested, but you should get the general idea):

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 覆盖类型).你得到一个 JSONValue,检查它是否真的是你认为它应该是的,比如说,一个 JSONObject,你得到那个 JSONObject,检查它是否有xyz"键,你会得到一个 JSONValue,冲洗并重复.不是最有趣的工作,但至少它比在整个 JSON 上调用 eval() 更安全:)
注意: 正如 Jason 指出的那样,在 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 作为 JSON 解析器通过 JSNI.

As you can see, when using JSONParser you have to/should be very cautious - that's the whole point, right? To parse an unsafe JSON (otherwise, like I suggested in the comments, you should go with JavaScript Overlay Types). You get a JSONValue, check if it's really what you think it should be, say, a JSONObject, you get that JSONObject, check if it has the "xyz" key, you get a JSONValue, rinse and repeat. Not the most interesting work, but at least its safer than just calling eval() on the whole JSON :)
Attention: as Jason pointed out, prior to GWT 2.1, JSONParser used eval() internally (it only had a parse() method - GWT 2.0 javadocs vs GWT 2.1). In GWT 2.1, parse() became deprecated and two more methods were introduced - parseLenient() (uses eval() internally) and parseStrict() (the safe approach). If you really have to use JSONParser, then I'd suggest upgrading to GWT 2.1 M2, because otherwise you might as well use JSOs. As an alternative to JSONParser for untrusted sources, you could try integrating json2.js as a JSON parser via JSNI.

PS: cinqoTimo, JSONArray jsonValue = JSONParser.parse(incomingJsonRespone); 显然不起作用,因为 JSONParser.parseJSONValue 的返回类型,而不是 JSONArray - 您的 IDE(Eclipse + Google 插件?)没有警告过您吗?或者至少是编译器.

PS: cinqoTimo, JSONArray jsonValue = JSONParser.parse(incomingJsonRespone); obviously doesn't work because JSONParser.parse has a return type of JSONValue, not JSONArray - didn't your IDE (Eclipse + Google Plugin?) warn you? Or at least the compiler.

这篇关于GWT:处理传入的 JSON 字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆