1

我正在使用 Jackson 编写数据并在客户端使用 gwt-json 来解析数据。我正在客户端通过字典渲染数据,这很慢,所以我想加快解析速度。我尝试使用 eval() 而不是 Dictionary 的 JSNI,它在速度方面与字典相比做得很好,但它存在安全问题。

 private List<Party> getPartyListFormJackson() {    

    String name = "partyListInfo"; // jsp variable     
    String partyListStr = getString(name);     
    JSONArray partyJSONArray = JSONParser.parse(partyListStr).isArray();    
    for(int i=0; i < partyJSONArray.size(); i++) {    
      JSONObject partyJSONObject = (JSONObject) partyJSONArray.get(i);   
      Party party = VsJacksonFactory.getParty(partyJSONObject);    
      partyList.add(party);     
    }    
    return partyList;    
  }    

  public static native String getString(String name) /*-{     
  return eval('$wnd.'+ name);     
}-*/;    

在上面我可以使用哪种方法代替 eval()?

任何人都可以对此有任何想法,以使其变得更好。

谢谢MSNaidu

4

1 回答 1

1

GWT has basically 3 ways to parse JSON (this is without third-party libs):

  • JSONParser is the legacy API. It's cumbersome to work with but is most flexible, particularly when you work with dynamic JSON (where the structure is not fixed in advance)
  • JsonUtils.safeEval() with JavaScriptObjects (aka overlay types) is the most lightweight; it'll use native JSON.parse() where supported, and will validate the JSON with a regexp before handing it to eval() otherwise
  • AutoBeans is the latest addition. It works in GWT and in the JVM (server-side, Android clients, etc.)

All approaches can also work when you have a JavaScriptObject instance rather than a JSON string (you mention Dictionary, which is not about JSON; this is why I add this precision).

于 2013-04-08T09:55:27.920 回答