将Json对象转换为java对象:逻辑

时间:2014-05-13 04:13:32

标签: java json

我有一个json天气API,预测每三个小时五天。我把json的一部分放在两个预测中,因为它太长而且重复:

{

    "cod": "200",
    "message": 0.4194,
    "city": {
        "id": 1851632,
        "name": "Shuzenji",
        "coord": {
            "lon": 138.933334,
            "lat": 34.966671
        },
        "country": "JP",
        "population": 0
    },
    "cnt": 40,
    "list": [
        {
            "dt": 1399950000,
            "main": {
                "temp": 287.82,
                "temp_min": 287.82,
                "temp_max": 287.82,
                "pressure": 923.74,
                "sea_level": 1018.93,
                "grnd_level": 923.74,
                "humidity": 100,
                "temp_kf": 0
            },
            "weather": [
                {
                    "id": 501,
                    "main": "Rain",
                    "description": "moderate rain",
                    "icon": "10d"
                }
            ],
            "clouds": {
                "all": 92
            },
            "wind": {
                "speed": 0.51,
                "deg": 226.005
            },
            "rain": {
                "3h": 6
            },
            "sys": {
                "pod": "d"
            },
            "dt_txt": "2014-05-13 03:00:00"
        },
        {
            "dt": 1399960800,
            "main": {
                "temp": 291.36,
                "temp_min": 291.358,
                "temp_max": 291.36,
                "pressure": 921.65,
                "sea_level": 1016.09,
                "grnd_level": 921.65,
                "humidity": 87,
                "temp_kf": 0
            },
            "weather": [
                {
                    "id": 500,
                    "main": "Rain",
                    "description": "light rain",
                    "icon": "10d"
                }
            ],
            "clouds": {
                "all": 20
            },
            "wind": {
                "speed": 0.87,
                "deg": 12.0018
            },
            "rain": {
                "3h": 0.5
            },
            "sys": {
                "pod": "d"
            },
            "dt_txt": "2014-05-13 06:00:00"
        },

我的问题是我不了解制作java对象的逻辑。我使用jsongen生成java对象,但似乎它没有给我正确的对象。 通过jsongen我的java对象类是:

public class Jweather {

        private Number cod;
        private Number message;
        private City city;
        private Number cnt;
        private myList list;
                   ...
                 }

public class City{
    private Coord coord;
    private String country;
    private Number id;
    private String name;
    private Number population;
            ...
           }

public class Coord{
    private Number lat;
    private Number lon;
            ...
           }

public class myList{

    List list = new ArrayList();
    private Number clouds;
    private Number deg;
    private Number dt;
    private Number humidity;
    private Number pressure;
    private Number speed;
    private Temp temp;
    private List weather;
              ...
            }

public class Temp{
    private Number day;
    private Number eve;
    private Number max;
    private Number min;
    private Number morn;
    private Number night;
           ...
         }

public class Weather{
    private City city;
    private Number cnt;
    private String cod;
    private List list;
    private Number message;
            ...
         }

我认为myList,temp,weather class错了。我写错了吗? 我认为list.java应该包含dt,main,weather,clouds,wind,rain,sys和dt_txt 我还需要一个main.java类,包括:temp,temp_min,temp_max,pressure,sea_level,grnd_level,humidity和temp_kf 和一个cloud.java类,包括:all 和风类包括:速度,度 和雨班包括:3h 和一个sys类,包括:pob

任何帮助都将受到高度赞赏。

1 个答案:

答案 0 :(得分:0)

根据您粘贴的JSON,我同意生成的类看起来不正确。例如,morn似乎根本没有出现在JSON中。您是否粘贴了用于生成类的整个JSON内容?

您使用什么来序列化/反序列化JSON? Jackson是一个用来做到这一点的通用框架。您需要从存储反序列化JSON的bean中定义映射器到POJO bean。

我自己没有使用它(我使用Jackson并自己创建JSON和POJO bean),但基于json gen doc,它似乎是预期的生成JSON-bean方面的东西。所以你仍然需要(至少可能)定义你的POJO bean(除非他们碰巧100%映射,这似乎不太可能由于各种原因,从不同的JSON属性名称与POJO字段名称等)。