使用 GSON 将 2D JSON 数组反序列化为 bean

发布于 2024-12-12 05:45:53 字数 2695 浏览 0 评论 0 原文

我无法将此嵌套数组 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);

I have not been able to map this nested array rows->elements to my javabean. Is gson actually capable of handling this kind of mapping? I also tried a different approach, which you can see if you look at the commented out Java code.

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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

高跟鞋的旋律 2024-12-19 05:45:53

这应该是您的 GeoZipCodesBean2 对象的 JSON 格式(如果 rowsList>

{
    "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());
}

This should be the JSON format for your GeoZipCodesBean2 object (if rows is a List<List<Elem>>)

{
    "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"
                    }
                ]
            }
        ]
    ]
}

This is the code for converting to/from 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());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文