【问题标题】:Deserializing 2D JSON array to bean using GSON使用 GSON 将 2D JSON 数组反序列化为 bean
【发布时间】:2011-10-28 04:56:13
【问题描述】:

我无法将此嵌套数组 rows->elements 映射到我的 javabean。 gson 真的能够处理这种映射吗?我还尝试了一种不同的方法,您可以查看注释掉的 Java 代码。

package scratch;

import java.util.ArrayList;
import java.util.List;

/*
{
  "rows" : [
  {
     "elements" : [
        {
           "distance" : {
              "text" : "897 mi",
              "value" : 1443464
           },
           "duration" : {
              "text" : "14 hours 32 mins",
              "value" : 52327
           },
           "status" : "OK"
        }
     ]
  },
  {
     "elements" : [
        {
           "distance" : {
              "text" : "378 mi",
              "value" : 607670
           },
           "duration" : {
              "text" : "6 hours 22 mins",
              "value" : 22908
           },
           "status" : "OK"
        }
     ]
  }
]

}
*/

public class GeoZipCodesBean2 {

    //    private Elem[][] rows;

    //    public Elem[][] getRows() {
    //        return rows;
    //    }
    //
    //    public void setRows(Elem[][] rows) {
    //        this.rows = rows;
    //    }

    private List<List<Elem>>rows;

    public List<List<Elem>> getRows() {
        return rows;
    }

    public void setRows(List<List<Elem>> rows) {
        this.rows = rows;
    }



    public static class Elem {
        private Distance distance;
        private Duration duration;

        public Distance getDistance() {
            return distance;
        }

        public void setDistance(Distance distance) {
            this.distance = distance;
        }

        public Duration getDuration() {
            return duration;
        }

        public void setDuration(Duration duration) {
            this.duration = duration;
        }
    }

    public static class Distance {
        private String text;
        private Integer value;

        public String getText() {
            return text;
        }

        public void setText(String text) {
            this.text = text;
        }

        public Integer getValue() {
            return value;
        }

        public void setValue(Integer value) {
            this.value = value;
        }
    }

    public static class Duration {
        private String text;
        private Integer value;

        public String getText() {
            return text;
        }

        public void setText(String text) {
            this.text = text;
        }

        public Integer getValue() {
            return value;
        }

        public void setValue(Integer value) {
            this.value = value;
        }
    }
}

GeoZipCodesBean2 geoZipCodesBean2 = new Gson().fromJson(str, GeoZipCodesBean2.class);

【问题讨论】:

  • 查看您的 json,rows 指向元素列表而不是元素列表。可能是json生成部分有问题。此外,json 中有一些错误。最后有一个逗号。你可以在这里验证你的 json jsonlint.com
  • @Narendra, rows 是一个数组, elements 也是一个数组。所以它实际上是一个二维数组。
  • 您的代码显示rowsList&lt;List&lt;Element&gt;&gt;

标签: java json multidimensional-array gson


【解决方案1】:

这应该是您的 GeoZipCodesBean2 对象的 JSON 格式(如果 rowsList&lt;List&lt;Elem&gt;&gt;

{
    "rows": [
        [
            {
                "elements": [
                    {
                        "distance": {
                            "text": "897 mi",
                            "value": 1443464
                        },
                        "duration": {
                            "text": "14 hours 32 mins",
                            "value": 52327
                        },
                        "status": "OK"
                    }
                ]
            },
            {
                "elements": [
                    {
                        "distance": {
                            "text": "378 mi",
                            "value": 607670
                        },
                        "duration": {
                            "text": "6 hours 22 mins",
                            "value": 22908
                        },
                        "status": "OK"
                    }
                ]
            }
        ]
    ]
}

这是与json相互转换的代码

public static void main(String[] args) {
    Gson gson = new GsonBuilder().create();
    GeoZipCodesBean2 geo = new GeoZipCodesBean2();
    List<List<Elem>> rows = new ArrayList<List<Elem>>();
    List<Elem> elem = new ArrayList<Elem>();
    Elem e1 = new Elem();
    Distance d = new Distance();
    d.setText("fads");
    d.setValue(1234);
    e1.setDistance(d);
    elem.add(e1);
    rows.add(elem);
    geo.setRows(rows);
    String json = gson.toJson(geo);
    //The following prints {"rows":[[{"distance":{"text":"fads","value":1234}}]]}
    System.out.println(json);
    json = "{\"rows\":[[{\"distance\":{\"text\":\"fads\",\"value\":1234}, \"status\":\"OK\"}]]}";
    GeoZipCodesBean2 geo2 = gson.fromJson(json, GeoZipCodesBean2.class);
    //The following prints 1234
    System.out.println(geo2.getRows().get(0).get(0).getDistance().getValue());
}

【讨论】:

  • 你是对的。这个 JSON 对于我当前的代码是正确的,这意味着我的 Java 代码对于原始 JSON 是错误的。实际上,最初我的实例变量是“私有 Listrows”,但即使这两个 Elem 被 GSON 实例化,它们内部的距离和持续时间仍为空 - 我不知道为什么会发生这种情况。使用 gson 时是否必须将 JSON 中的所有内容映射到 bean?
  • 其实还有其他字段我省略了。除了我放在“Elem”中的字段(距离和持续时间)之外,一切都被初始化了。
  • @Onur 添加了代码,看起来距离对象正在正确初始化。
  • 这不适用于原始 JSON。这不适用于我发布的原始 JSON。我当前的代码与“Listrows”在这里link。我需要它来处理原始 JSON。这是我的测试课link
  • @Onur 问题是你的 json 和你的对象结构不匹配。修复 json 问题后,代码将如下所示 pastebin.com/h1fv4wzV 。您必须更改对象结构以匹配 Google 地图 api 返回的 json。
猜你喜欢
  • 1970-01-01
  • 2015-02-24
  • 2011-03-28
  • 2017-07-31
  • 2017-01-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多