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

时间:2011-10-28 04:56:13

标签: java json multidimensional-array gson

我无法将此嵌套数组rows->元素映射到我的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);

1 个答案:

答案 0 :(得分:1)

这应该是GeoZipCodesBean2对象的JSON格式(如果rowsList<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的代码

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