Gson无法正确将LinkedHashMap转换为JSON

dddzy1tm  于 2023-11-18  发布在  其他
关注(0)|答案(1)|浏览(223)

当我使用GSON将LinkedHashMap直接转换为JSON时,它可以正确转换,但是当我将其转换为class字段时,它不能正确转换。在我正在处理的另一个案例中,只有LinkedHashMap的最后一个元素出现在最终的JSON中。

public class Example {
    public static void main(String[] args) {
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        System.out.println("GSON HashMap!");
        System.out.println(gson.toJson(hashMe()));
        System.out.println("GSON Zone!");

        //This doesn't give me the full LinkedHashMap
        System.out.print(gson.toJson(zoneMe()));
    }
    
    public ZoneChild zoneMe() {
        RateChild rate = new RateChild(hashMe());
        ZoneChild zone = new ZoneChild();
        zone.setName("Example");
        zone.setRate(rate);
        return zone;
    }
    
    public LinkedHashMap<Integer, String> hashMe() {
        LinkedHashMap<Integer, String> rates = new LinkedHashMap<Integer, String>();
        rates.put(60, "data1");
        rates.put(25, "data2");
        rates.put(15, "data3");
        
        return rates;
    }
    
    public class Zone {
        RateChild rate;
        
        public Zone() {
        }
        
        public void setRate(RateChild rate) {
            this.rate = rate;
        }
    }
    
    public class ZoneChild extends Zone {
        private int level;
        private String name;
        
        public ZoneChild() {    
        }
        
        public void setName(String name) {
            this.name = name;
        }
        
        public void setLevel(int level) {
            this.level = level;
        }
    }
    
    public class Rate {
        LinkedHashMap<Integer, String> rate = new LinkedHashMap<Integer, String>();
        
        public Rate(LinkedHashMap<Integer, String> rate) {
            this.rate = rate;
        }
    }

    //I need this class for specialization
    public class RateChild extends Rate {
        
        public RateChild(LinkedHashMap<Integer, String> rate) {
            super(rate);
        }
    }
}

字符串
输出如下所示:

GSON HashMap!
{
  "60": "data1",
  "25": "data2",
  "15": "data3"
}
GSON Zone!
{
  "level": 0,
  "name": "Example",
  "rate": {
    "rate": {}
  }
}


我注意到,当LinkedHashMap字段直接位于JSON转换类(ZoneChild)中时,它不会失败。

csga3l58

csga3l581#

无法重现。使用Java 2.10.1和Gson 2.10.1,以下代码按预期工作:

import java.util.LinkedHashMap;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class Main {

    public static void main(String[] args) {
        LinkedHashMap<Integer, String> data = new LinkedHashMap<>();
        data.put(60, "data1");
        data.put(25, "data2");
        data.put(15, "data3");

        Zone zone = new ZoneChild(new RateChild(data), "Example", 0);
        System.out.printf("##########  Original   ##########%n%n%s%n%n", zone);

        Gson gson = new GsonBuilder().setPrettyPrinting().create();

        String jsonStr = gson.toJson(zone);
        System.out.printf("########## JSON String ##########%n%n%s%n%n", jsonStr);

        Zone decoded = gson.fromJson(jsonStr, ZoneChild.class);
        System.out.printf("##########   Decoded   ##########%n%n%s%n", decoded);
    }

    public static class Zone {

        RateChild rate;

        public Zone() {}

        public Zone(RateChild rate) {
            this.rate = rate;
        }

        @Override
        public String toString() {
            return String.format("Zone[rate=%s]", rate);
        }
    }

    public static class ZoneChild extends Zone {

        String name;
        int level;

        public ZoneChild() {}

        public ZoneChild(RateChild rate, String name, int level) {
            super(rate);
            this.name = name;
            this.level = level;
        }

        @Override
        public String toString() {
            return String.format("ZoneChild[rate=%s, name=%s, level=%d]", rate, name, level);
        }
    }

    public static class Rate {

        LinkedHashMap<Integer, String> data;

        public Rate() {}

        public Rate(LinkedHashMap<Integer, String> data) {
            this.data = data;
        }

        @Override
        public String toString() {
            return String.format("Rate[data=%s]", data);
        }
    }

    public static class RateChild extends Rate {

        public RateChild() {}

        public RateChild(LinkedHashMap<Integer, String> data) {
            super(data);
        }

        @Override
        public String toString() {
            return String.format("RateChild[data=%s]", data);
        }
    }
}

字符串
输出量:

##########  Original   ##########

ZoneChild[rate=RateChild[data={60=data1, 25=data2, 15=data3}], name=Example, level=0]

########## JSON String ##########

{
  "name": "Example",
  "level": 0,
  "rate": {
    "data": {
      "60": "data1",
      "25": "data2",
      "15": "data3"
    }
  }
}

##########   Decoded   ##########

ZoneChild[rate=RateChild[data={60=data1, 25=data2, 15=data3}], name=Example, level=0]

相关问题