使用GSON将2D JSON数组反序列化为bean [英] Deserializing 2D JSON array to bean using GSON

查看:140
本文介绍了使用GSON将2D JSON数组反序列化为bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法将这个嵌套数组的rows->元素映射到我的javabean。 gson实际上能够处理这种映射吗?我还尝试了一种不同的方法,您可以看看是否看到注释掉的Java代码。

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


推荐答案

这应该是您的<$ c的JSON格式$ c> GeoZipCodesBean2 对象(如果 rows List< List< Elem>>

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

这是转换为/来自json的代码

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

这篇关于使用GSON将2D JSON数组反序列化为bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆