代码之家  ›  专栏  ›  技术社区  ›  tpow

gwt:处理传入的JSON字符串

  •  10
  • tpow  · 技术社区  · 15 年前

    我正在开发一个GWT应用程序,它正在接收一个JSON字符串,我很难理解每个对象的值。我正在尝试将传入的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 正在创建实例。我尝试这样做的方式有什么明显的错误吗,因为我在错误消息方面没有得到太多帮助?

    2 回复  |  直到 7 年前
        1
  •  18
  •   Community CDub    8 年前

    回应莫弗里西的评论:
    实际上,它更复杂:它看起来像这样(代码未经测试,但您应该了解一般的想法):

    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 你明白了 JSON对象 ,检查它是否有“xyz”键,您会得到一个 JSON值 冲洗并重复。不是最有趣的工作,但至少比打电话更安全 eval() 总的来说,json:)
    注意: 正如杰森指出的,在GWT 2.1之前, JSONPARSER 习惯于 表达式() 内部(它只有一个 parse() 方法- GWT 2.0 JavaDoc vs GWT 2.1 )在GWT 2.1中, PARSE() 被弃用,引入了另外两种方法- parseLenient() (使用) 表达式() 内部)和 parseStrict() (安全方法)。如果你真的要用 JSONPARSER ,那么我建议升级到GWT2.1 m2,否则您也可以使用JSO。作为替代 JSONPARSER 对于不受信任的源,可以尝试集成 json2.js as a JSON parser via JSNI .

    皮克斯: JSONArray jsonValue = JSONParser.parse(incomingJsonRespone); 显然不起作用,因为 JSONParser.parse 返回类型为 JSON值 不是 JSONArray -你的IDE(Eclipse+Google插件)不是吗?警告你?或者至少是编译器。

        2
  •  2
  •   RMorrisey    15 年前

    看起来您没有数组,只有一个根对象,它的属性“d”是一个数组。我不熟悉那个特定的API,但也许您可以尝试检索JSONObject或类似的对象,而不是数组?