Java 动态json的解析
我有一个json,我正试图用Jackson解析它。JSON看起来像-Java 动态json的解析,java,json,jackson,Java,Json,Jackson,我有一个json,我正试图用Jackson解析它。JSON看起来像- coupons: { 1: { title: "Mode von", description: "", type: "offer", code: "-", expiry: "0000-00-00", link: "" }, 2: { title: "Prime 1", description: "", type: "o
coupons: {
1: {
title: "Mode von",
description: "",
type: "offer",
code: "-",
expiry: "0000-00-00",
link: ""
},
2: {
title: "Prime 1",
description: "",
type: "offer",
code: "-",
expiry: "0000-00-00",
link: "http://test.com/"
}
}
优惠券的数量在这里并不是固定不变的,而且会因响应的不同而不同。
我的难题是创建相应的java类来容纳这样的对象
我试着用地图作为工具-
public class Coupons {
Map<String, String>coupons = new HashMap<String, String>();
public Map<String, String> getCoupons() {
return coupons;
}
}
总是给我空的。对于这个json,什么是正确的java类?您的第一级键是索引号1、2、3等等。 因此,为了获得类型和代码,您必须指定密钥 您可以这样做:
var coupons = coupons.getCoupons(); //<--breakpoint to see if this is really populated.
foreach( String key in coupons.Keys ){ //<- pseudo code, iterate over keys
var obj = coupons.get(key);
var type = obj.get("type");
etc..
}
var-tuops=tuops.gettuops()//我想我不能只做-maptups=newhashmap()
ascoups.getcoups()
没有为我获取任何映射对象OK,我将映射更改为-private-maptups=new-HashMap()代码>但解决方案的关键是按照您的建议获取地图的键。
var coupons = coupons.getCoupons(); //<--breakpoint to see if this is really populated.
foreach( String key in coupons.Keys ){ //<- pseudo code, iterate over keys
var obj = coupons.get(key);
var type = obj.get("type");
etc..
}