Gson:java.util.Map转换所有键值对

Gson:java.util.Map转换所有键值对,java,json,gson,Java,Json,Gson,我有以下代码: Collection<Product> products = productRepository.findProducts(from, pageSize); Map<Product, BigDecimal> map = new TreeMap<>(new ProductComparator()); for (Product product : products) { BigDecimal buyPrice =

我有以下代码:

Collection<Product> products = productRepository.findProducts(from, pageSize);

    Map<Product, BigDecimal> map = new TreeMap<>(new ProductComparator());
    for (Product product : products) {
        BigDecimal buyPrice = warehouseView.find(product.getId()).getBuyPrice();
        map.put(product, buyPrice);
    }

    Gson gson = new Gson();
    String json = gson.toJson(map);
    TypeToken mapType = new TypeToken<Map<Product, BigDecimal>>(){}.getType();
    String json2 = gson.toJson(map, mapType);
    JsonElement json3 = gson.toJsonTree(map, mapType);
Collection products=productRepository.findProducts(from,pageSize);
Map Map=newtreemap(newproductcomparator());
用于(产品:产品){
BigDecimal buyPrice=warehouseView.find(product.getId()).getBuyPrice();
map.put(产品、买价);
}
Gson Gson=新的Gson();
字符串json=gson.toJson(map);
TypeToken mapType=新的TypeToken(){}.getType();
字符串json2=gson.toJson(map,mapType);
JsonElement json3=gson.toJsonTree(map,mapType);
解释:我从DB退回了5种产品,然后我给DB打电话了解每种产品的购买价格。DB可能返回0-表示仓库中不存在给定产品

那么我想要实现的是:获取json对象,它包含所有5种产品

现在是什么:例如,如果3个产品的DB返回0-我将只得到2个转换为json的产品

我尝试过不同版本的转换,例如
json
json2
json3
,但都没有成功


那么,如何转换具有
0
值的完整映射对象呢?

您确定您的服务没有返回空值,并且/或者您确定某些条目的产品不是空值吗

    Map<String, BigDecimal> test = new TreeMap<>();
    test.put("Test", BigDecimal.ZERO);
    test.put("Test2", BigDecimal.ONE);
    test.put("Test3", null);

    Gson gson = new Gson();

    Type mapType = new TypeToken<HashMap<String, Integer>>() {}.getType();
    System.out.println(gson.toJson(test, mapType));

    Gson gson2 = new GsonBuilder().serializeNulls().create();
    System.out.println(gson2.toJson(test, mapType));

你也可以试试下面的方法

List<Map<String, Object>> mapdataList = new ArrayList<Map<String, Object>>();

Map<String, Object> MapObj = new HashMap<String, Object>();
MapObj.put("windowHandle", "current");
MapObj.put("id", "{a67fc38c-10e6-4c5e-87dc-dd45134db570}");
mapdataList.add(MapObj);

Gson gson = new Gson();
JsonObject jObject = new JsonObject();
JsonParser jP = new JsonParser();
jObject.add("data", jP.parse(gson.toJson(mapdataList)));
System.out.println(jObject.toString());
List-mapdataList=new-ArrayList();
Map MapObj=新的HashMap();
MapObj.put(“窗口句柄”、“当前”);
MapObj.put(“id”,“{a67fc38c-10e6-4c5e-87dc-dd45134db570}”);
mapdataList.add(MapObj);
Gson Gson=新的Gson();
JsonObject jObject=新的JsonObject();
JsonParser jP=新的JsonParser();
add(“data”,jP.parse(gson.toJson(mapdataList));
System.out.println(jObject.toString());

对于0,BigDecimal是否返回为NULL?是的,就是这样。愚蠢的错误。谢谢你的关注!
List<Map<String, Object>> mapdataList = new ArrayList<Map<String, Object>>();

Map<String, Object> MapObj = new HashMap<String, Object>();
MapObj.put("windowHandle", "current");
MapObj.put("id", "{a67fc38c-10e6-4c5e-87dc-dd45134db570}");
mapdataList.add(MapObj);

Gson gson = new Gson();
JsonObject jObject = new JsonObject();
JsonParser jP = new JsonParser();
jObject.add("data", jP.parse(gson.toJson(mapdataList)));
System.out.println(jObject.toString());