关于JSON键名重复的问题,如何解决?

用Swing+和风天气API完成项目时遇到JSON键名重复的问题,如何读取对应的数据并显示到GUI窗口中

String uri2="https://devapi.qweather.com/v7/weather/7d?location="+cityCode+"&key="+KEY;
String result2=getHttpResult(uri2);//获取到的JSON数据

JSON数据如下:

{
  "code": "200",
  "updateTime": "2021-11-15T16:35+08:00",
  "fxLink": "http://hfx.link/2ax1",
  "daily": [
    {
      "fxDate": "2021-11-15",
      "sunrise": "06:58",
      "sunset": "16:59",
      "moonrise": "15:16",
      "moonset": "03:40",
      "moonPhase": "盈凸月",
      "moonPhaseIcon": "803",
      "tempMax": "12",
      "tempMin": "-1",
      "iconDay": "101",
      "textDay": "多云",
      "iconNight": "150",
      "textNight": "晴",
      "wind360Day": "45",
      "windDirDay": "东北风",
      "windScaleDay": "1-2",
      "windSpeedDay": "3",
      "wind360Night": "0",
      "windDirNight": "北风",
      "windScaleNight": "1-2",
      "windSpeedNight": "3",
      "humidity": "65",
      "precip": "0.0",
      "pressure": "1020",
      "vis": "25",
      "cloud": "4",
      "uvIndex": "3"
    },

返回数据说明如下:
code API状态码,具体含义请参考状态码
updateTime 当前API的最近更新时间

上述数据描述不全,详见和风天气开发文档:

https://dev.qweather.com/docs/api/weather/weather-daily-forecast/

想到的解决方式:将JSON数据转为Java对象,但get到的数据全为null
望指导
(˚ ˃̣̣̥᷄⌓˂̣̣̥᷅ )

get到的数据全为null是指接口返回数据为null,还是经json转换之后的java对应字段属性为null?你的json转换为java对象用的哪个类库?fastjson? jackson? gson?

针对解析 Json 处理重复键名的问题,我给你提供如下关于 jackson,fastjson,org.json 的处理方案地址:

jackson 处理方案:
https://betheme.net/yidongkaifa/37902.html?action=onClick

fastJson 处理方案:

https://blog.csdn.net/Noreaday/article/details/88078680

org.json 处理方案:
https://blog.csdn.net/weixin_63949796/article/details/128559269

你可以使用 JSONObject 来读取 JSON 数据,然后通过调用 get() 方法获取对应的值。例如,要获取 "code" 字段的值,你可以这样写:

Copy code
JSONObject json = new JSONObject(result2);
String code = json.getString("code");
如果 "daily" 字段是一个数组,你可以这样获取它的第一个元素:

Copy code
JSONArray daily = json.getJSONArray("daily");
JSONObject day1 = daily.getJSONObject(0);
然后你就可以使用 get() 方法读取 "day1" 对象中的其他字段。例如,获取 "fxDate" 字段:

Copy code
String fxDate = day1.getString("fxDate");
最后,你可以使用 Swing 的组件显示这些数据。例如,将 "fxDate" 显示在文本框中:

Copy code
textField.setText(fxDate);

望采纳!!!点击回答右侧采纳即可!!

你可以使用 JSONObject 来读取 JSON 数据,然后通过调用 get() 方法获取对应的值。

JSONObject json = new JSONObject(result2);
String code = json.getString("code");

如果 "daily" 字段是一个数组,你可以这样获取它的第一个元素:

JSONArray daily = json.getJSONArray("daily");
JSONObject day1 = daily.getJSONObject(0);

你可以考虑使用 JSONObject 或者 JSONArray 的 getString() 方法来获取对应键的值。例如:

JSONObject json = new JSONObject(result2);
String code = json.getString("code");
String updateTime = json.getString("updateTime");

// 获取 daily 数组
JSONArray daily = json.getJSONArray("daily");

// 获取 daily 数组的第一个元素
JSONObject day1 = daily.getJSONObject(0);
String fxDate = day1.getString("fxDate");
String sunrise = day1.getString("sunrise");
...

如果每天的信息数据结构相同,你也可以考虑定义一个类来存储每天的信息,然后使用 JSONObject 的 get() 方法来获取对应键的值并存储到类的对象中。例如:

public class Day {
    private String fxDate;
    private String sunrise;
    private String sunset;
    ...

    public Day(JSONObject json) {
        this.fxDate = json.getString("fxDate");
        this.sunrise = json.getString("sunrise");
        this.sunset = json.getString("sunset");
        ...
    }
}

JSONObject json = new JSONObject(result2);
JSONArray daily = json.getJSONArray("daily");

List<Day> days = new ArrayList<>();
for (int i = 0; i < daily.length(); i++) {
    JSONObject day = daily.getJSONObject(i);
    Day d = new Day(day);
    days.add(d);
}

参考如下实体类,然后使用fastjson进行json字符串转实体类



package cn.json.pojo;
import java.util.Date;
import java.util.List;


public class JsonRootBean {

    private String code;
    private Date updateTime;
    private String fxLink;
    private List<Daily> daily;
    public void setCode(String code) {
         this.code = code;
     }
     public String getCode() {
         return code;
     }

    public void setUpdateTime(Date updateTime) {
         this.updateTime = updateTime;
     }
     public Date getUpdateTime() {
         return updateTime;
     }

    public void setFxLink(String fxLink) {
         this.fxLink = fxLink;
     }
     public String getFxLink() {
         return fxLink;
     }

    public void setDaily(List<Daily> daily) {
         this.daily = daily;
     }
     public List<Daily> getDaily() {
         return daily;
     }

}


package cn.json.pojo;
import java.util.Date;


public class Daily {

    private Date fxDate;
    private String sunrise;
    private String sunset;
    private String moonrise;
    private String moonset;
    private String moonPhase;
    private String moonPhaseIcon;
    private String tempMax;
    private String tempMin;
    private String iconDay;
    private String textDay;
    private String iconNight;
    private String textNight;
    private String wind360Day;
    private String windDirDay;
    private Date windScaleDay;
    private String windSpeedDay;
    private String wind360Night;
    private String windDirNight;
    private Date windScaleNight;
    private String windSpeedNight;
    private String humidity;
    private String precip;
    private String pressure;
    private String vis;
    private String cloud;
    private String uvIndex;
    public void setFxDate(Date fxDate) {
         this.fxDate = fxDate;
     }
     public Date getFxDate() {
         return fxDate;
     }

    public void setSunrise(String sunrise) {
         this.sunrise = sunrise;
     }
     public String getSunrise() {
         return sunrise;
     }

    public void setSunset(String sunset) {
         this.sunset = sunset;
     }
     public String getSunset() {
         return sunset;
     }

    public void setMoonrise(String moonrise) {
         this.moonrise = moonrise;
     }
     public String getMoonrise() {
         return moonrise;
     }

    public void setMoonset(String moonset) {
         this.moonset = moonset;
     }
     public String getMoonset() {
         return moonset;
     }

    public void setMoonPhase(String moonPhase) {
         this.moonPhase = moonPhase;
     }
     public String getMoonPhase() {
         return moonPhase;
     }

    public void setMoonPhaseIcon(String moonPhaseIcon) {
         this.moonPhaseIcon = moonPhaseIcon;
     }
     public String getMoonPhaseIcon() {
         return moonPhaseIcon;
     }

    public void setTempMax(String tempMax) {
         this.tempMax = tempMax;
     }
     public String getTempMax() {
         return tempMax;
     }

    public void setTempMin(String tempMin) {
         this.tempMin = tempMin;
     }
     public String getTempMin() {
         return tempMin;
     }

    public void setIconDay(String iconDay) {
         this.iconDay = iconDay;
     }
     public String getIconDay() {
         return iconDay;
     }

    public void setTextDay(String textDay) {
         this.textDay = textDay;
     }
     public String getTextDay() {
         return textDay;
     }

    public void setIconNight(String iconNight) {
         this.iconNight = iconNight;
     }
     public String getIconNight() {
         return iconNight;
     }

    public void setTextNight(String textNight) {
         this.textNight = textNight;
     }
     public String getTextNight() {
         return textNight;
     }

    public void setWind360Day(String wind360Day) {
         this.wind360Day = wind360Day;
     }
     public String getWind360Day() {
         return wind360Day;
     }

    public void setWindDirDay(String windDirDay) {
         this.windDirDay = windDirDay;
     }
     public String getWindDirDay() {
         return windDirDay;
     }

    public void setWindScaleDay(Date windScaleDay) {
         this.windScaleDay = windScaleDay;
     }
     public Date getWindScaleDay() {
         return windScaleDay;
     }

    public void setWindSpeedDay(String windSpeedDay) {
         this.windSpeedDay = windSpeedDay;
     }
     public String getWindSpeedDay() {
         return windSpeedDay;
     }

    public void setWind360Night(String wind360Night) {
         this.wind360Night = wind360Night;
     }
     public String getWind360Night() {
         return wind360Night;
     }

    public void setWindDirNight(String windDirNight) {
         this.windDirNight = windDirNight;
     }
     public String getWindDirNight() {
         return windDirNight;
     }

    public void setWindScaleNight(Date windScaleNight) {
         this.windScaleNight = windScaleNight;
     }
     public Date getWindScaleNight() {
         return windScaleNight;
     }

    public void setWindSpeedNight(String windSpeedNight) {
         this.windSpeedNight = windSpeedNight;
     }
     public String getWindSpeedNight() {
         return windSpeedNight;
     }

    public void setHumidity(String humidity) {
         this.humidity = humidity;
     }
     public String getHumidity() {
         return humidity;
     }

    public void setPrecip(String precip) {
         this.precip = precip;
     }
     public String getPrecip() {
         return precip;
     }

    public void setPressure(String pressure) {
         this.pressure = pressure;
     }
     public String getPressure() {
         return pressure;
     }

    public void setVis(String vis) {
         this.vis = vis;
     }
     public String getVis() {
         return vis;
     }

    public void setCloud(String cloud) {
         this.cloud = cloud;
     }
     public String getCloud() {
         return cloud;
     }

    public void setUvIndex(String uvIndex) {
         this.uvIndex = uvIndex;
     }
     public String getUvIndex() {
         return uvIndex;
     }

}



Java如何解析带有重复key的Json
非常详细
https://blog.csdn.net/qq_45822970/article/details/111414052

将JSON数据转为Java对象,但get到的数据全为null,你这明显是转json获取有问题,json数据转对象的方式有很多,建议你可以用fastjson或者javaObject来处理。
fastjson教程:https://blog.csdn.net/cold___play/article/details/124525519
javaObject教程:https://blog.csdn.net/qq_48607414/article/details/125741047
希望能帮助到你,有用请采纳。

可以试试在线json对象一键转java实体类,然后再用json解析工具进行解析即可。

JSON转换成JavaBean实体对象,需要确保JavaBean中存在对应的getter和setter方法。你可以看下你的实体类中是否有添加getter和setter方法,如果采用的是lombok,还是为null,那可以试试自己生成setter和getter方法,看下是否还是为null,如果不为空null,那就说明lombok存在问题。