【问题标题】:Json String to ArrayJson 字符串到数组
【发布时间】:2014-08-29 16:41:43
【问题描述】:

我正在尝试将 JSON 字符串提取到 GWT 中的对象数组。

我的 JSON 字符串在这里:https://api.guildwars2.com/v1/wvw/matches.json

{"wvw_matches":[{"wvw_match_id":"2-6","red_world_id":2102,"blue_world_id":2105,"green_world_id":2205,"start_time":"2014-08-22T18:00:00Z","end_time":"2014-08-29T18:00:00Z"},
{"wvw_match_id":"1-2","red_world_id":1005,"blue_world_id":1009,"green_world_id":1016,"start_time":"2014-08-23T01:00:00Z","end_time":"2014-08-30T01:00:00Z"},
{"wvw_match_id":"1-6","red_world_id":1022,"blue_world_id":1012,"green_world_id":1018,"start_time":"2014-08-23T01:00:00Z","end_time":"2014-08-30T01:00:00Z"},
{"wvw_match_id":"2-4","red_world_id":2101,"blue_world_id":2014,"green_world_id":2204,"start_time":"2014-08-22T18:00:00Z","end_time":"2014-08-29T18:00:00Z"},
{"wvw_match_id":"2-9","red_world_id":2011,"blue_world_id":2008,"green_world_id":2006,"start_time":"2014-08-22T18:00:00Z","end_time":"2014-08-29T18:00:00Z"},
{"wvw_match_id":"1-5","red_world_id":1007,"blue_world_id":1002,"green_world_id":1004,"start_time":"2014-08-23T01:00:00Z","end_time":"2014-08-30T01:00:00Z"},
{"wvw_match_id":"2-3","red_world_id":2103,"blue_world_id":2012,"green_world_id":2003,"start_time":"2014-08-22T18:00:00Z","end_time":"2014-08-29T18:00:00Z"},
{"wvw_match_id":"2-1","red_world_id":2202,"blue_world_id":2002,"green_world_id":2010,"start_time":"2014-08-22T18:00:00Z","end_time":"2014-08-29T18:00:00Z"},
{"wvw_match_id":"1-1","red_world_id":1019,"blue_world_id":1017,"green_world_id":1008,"start_time":"2014-08-23T01:00:00Z","end_time":"2014-08-30T01:00:00Z"},
{"wvw_match_id":"2-5","red_world_id":2104,"blue_world_id":2301,"green_world_id":2013,"start_time":"2014-08-22T18:00:00Z","end_time":"2014-08-29T18:00:00Z"},
{"wvw_match_id":"2-8","red_world_id":2004,"blue_world_id":2001,"green_world_id":2009,"start_time":"2014-08-22T18:00:00Z","end_time":"2014-08-29T18:00:00Z"},
{"wvw_match_id":"2-7","red_world_id":2005,"blue_world_id":2207,"green_world_id":2206,"start_time":"2014-08-22T18:00:00Z","end_time":"2014-08-29T18:00:00Z"},
{"wvw_match_id":"1-3","red_world_id":1021,"blue_world_id":1003,"green_world_id":1014,"start_time":"2014-08-23T01:00:00Z","end_time":"2014-08-30T01:00:00Z"},
{"wvw_match_id":"2-2","red_world_id":2007,"blue_world_id":2203,"green_world_id":2201,"start_time":"2014-08-22T18:00:00Z","end_time":"2014-08-29T18:00:00Z"},
{"wvw_match_id":"1-4","red_world_id":1015,"blue_world_id":1023,"green_world_id":1011,"start_time":"2014-08-23T01:00:00Z","end_time":"2014-08-30T01:00:00Z"},
{"wvw_match_id":"1-7","red_world_id":1006,"blue_world_id":1001,"green_world_id":1010,"start_time":"2014-08-23T01:00:00Z","end_time":"2014-08-30T01:00:00Z"},
{"wvw_match_id":"1-8","red_world_id":1024,"blue_world_id":1020,"green_world_id":1013,"start_time":"2014-08-23T01:00:00Z","end_time":"2014-08-30T01:00:00Z"}]}

我的代码:

try {
      Request request = builder.sendRequest(null, new RequestCallback() {


        @Override
        public void onResponseReceived(Request request, Response response)
        {
             if (200 == response.getStatusCode()) {
                 Window.alert(response.getText());
                    updateTable(JsonUtils.<JsArray<JSONreceiverGW>>safeEval(response.getText()));
             }else{
                 Window.alert("Couldn't retrieve JSON " + response.getStatusText());

             }

        }

        @Override
        public void onError(Request request, Throwable exception)
        {
             Window.alert("Couldn't retrieve JSON");

        }
      });
    } catch (RequestException e) {
         Window.alert("Couldn't retrieve JSON");
    }

     private void updateTable(JsArray<JSONreceiverGW> jsArray) {

      for (int i = 0; i < jsArray.length(); i++) {
          Window.alert(jsArray.get(i).getmatchID());
        }


 }

我得到错误:

从 JSNI 方法返回的不是 int '@com.google.gwt.core.client.JsArray::length()': JS 类型值 未定义,预期 int

【问题讨论】:

    标签: java json gwt


    【解决方案1】:

    您收到的不是数组,它是一个对象,其属性的值为数组(并且该对象没有length 属性,因此是undefined)。您需要容器对象的 JSO:

    public class JSONreceiverGWContainer extends JavaScriptObject {
      protected JSONreceiverGWContainer() {}
    
      public final JsArray<JSONreceiverGW> getWvwMatches() /*-{ return this.wvw_matches; }-*/;
    }
    

    然后您将执行以下操作(为了便于阅读,分成两行):

    JSONreceiverGWContainer container = JsonUtils.safeEval(response.getText());
    updateTable(container.getWvwMatches());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多