英文:
Convert JSON to Java POJO
问题
I tried to use Jackson FasterXML library, ObjectMapper, to convert JSON to Java Pojo below, but could not find a way, for case like below.
{
"uniqueID1": {
"var1": true,
"var2": 0.0,
"var3": "cash",
"vars": {
"0": {
"varsvar1": "cash",
"varsvar2": {
"0": {
"varsvarvar1": "cash"
},
"1": {
"varsvarvar1": "cheque"
}
}
},
"1": {
"varsvar1": "cash",
"varsvar2": {
"0": {
"varsvarvar1": "cash"
},
"1": {
"varsvarvar1": "cheque"
}
}
}
}
},
"uniqueID2": {
"var1": true,
"var2": 0.0,
"var3": "cash",
"vars": {
"0": {
"varsvar1": "cash",
"varsvar2": {
"0": {
"varsvarvar1": "cash"
},
"1": {
"varsvarvar1": "cheque"
}
}
},
"1": {
"varsvar1": "cash",
"varsvar2": {
"0": {
"varsvarvar1": "cash"
},
"1": {
"varsvarvar1": "cheque"
}
}
}
}
},
.... can be a lot with different unique id ...
}
The unique ID 1 and unique ID 2 are unique, "0", "1", "0", "1" are also unique IDs, and there can be an unlimited number of unique IDs, so we can't use them as variable names. There are JSON objects inside JSON objects inside JSON objects, with a depth of 3.
I created 6 Java Pojo classes to represent each JSON structure. How to use ObjectMapper to convert the JSON above to these 6 classes below? Or is it not possible, and should we use another library?
public class Pojo1 {
private String uniqueIDPojo1;
private Pojo2 pojo2;
}
public class Pojo2 {
private boolean var1;
private double var2;
private String var3;
private Map<String, Pojo3> vars;
}
public class Pojo3 {
private String uniqueIDPojo3;
private Pojo4 pojo4;
}
public class Pojo4 {
private String varsvar1;
Map<String, Pojo5> varsvar2;
}
public class Pojo5 {
private String uniqueIDPojo5;
private Pojo6 pojo6;
}
public class Pojo6 {
private String varsvarvar1;
}
如何使用ObjectMapper将上面的JSON转换为下面的这6个类?或者这是否可能,我们应该使用其他库?
英文:
I tried to use Jackson FasterXML library, ObjectMapper,
to convert JSON to Java Pojo below,
but could not find a way, for case like below.
{
"uniqueID1": {
"var1": true,
"var2": 0.0,
"var3": "cash",
"vars": {
"0": {
"varsvar1": "cash",
"varsvar2": {
"0": {
"varsvarvar1": "cash"
},
"1": {
"varsvarvar1": "cheque"
}
}
},
"1": {
"varsvar1": "cash",
"varsvar2": {
"0": {
"varsvarvar1": "cash"
},
"1": {
"varsvarvar1": "cheque"
}
}
}
}
},
"uniqueID2": {
"var1": true,
"var2": 0.0,
"var3": "cash",
"vars": {
"0": {
"varsvar1": "cash",
"varsvar2": {
"0": {
"varsvarvar1": "cash"
},
"1": {
"varsvarvar1": "cheque"
}
}
},
"1": {
"varsvar1": "cash",
"varsvar2": {
"0": {
"varsvarvar1": "cash"
},
"1": {
"varsvarvar1": "cheque"
}
}
}
}
},
.... can be a lot with different unique id ...
}
The unique ID 1 and unique ID 2 are unique,
"0", "1", "0", "1" are also unique ID,
and there can be unlimited unique ID,
so we can't use it as variable name.
There are Json inside Json inside Json too, with 3 depth.
I created 6 Java Pojo classes to,
to represent each Json.
How to use ObjectMapper to convert the Json above to these 6 classes below ?
Or it is not possible, and we should use other Library ?
public class Pojo1{
private String uniqueIDPojo1;
private Pojo2 pojo2;
}
public class Pojo2{
private boolean var1;
private double var2;
private String var3;
private Map<String,Pojo3> vars;
}
public class Pojo3{
private String uniqueIDPojo3;
private Pojo4 pojo4;
}
public class Pojo4{
private String varsvar1;
Map<String,Pojo5> varsvar2;
}
public class Pojo5{
private String uniqueIDPojo5;
private Pojo6 pojo6;
}
public class Pojo6{
private String varsvarvar1;
}
答案1
得分: 0
Sure, here's the translation:
尝试使用
HashMap<String, Pojo2> pojos = new ObjectMapper().readValue(src, new TypeReference<HashMap<String, Pojo2>>() {});
英文:
try using
HashMap<String,Pojo2> pojos = new ObjectMapper().readValue(src, new TypeReference<HashMap<String,Pojo2>>() {});
答案2
得分: 0
它看起来对我来说像
```java
class VarMap {
Map<String, Var> varmap;
}
class Var {
boolean var1;
double var2;
String var3;
Map<String, VarVar> vars;
}
class VarVar {
String varsvar1;
Map<String, VarsVarVar> varsvar2;
}
class VarsVarVar {
String varsvarvar1;
}
你应该能够使用 objectMapper.readValue(in, VarMap.class)
进行解析。
<details>
<summary>英文:</summary>
It looks to me like
class VarMap {
Map<String, Var> varmap;
}
class Var {
boolean var1;
double var2;
String var3;
Map<String, VarVar> vars;
}
class VarVar {
String varsvar1;
Map<String, VarsVarVar> varsvar2;
}
class VarsVarVar {
String varsvarvar1;
}
which you should be able to parse with `objectMapper.readValue(in, VarMap.class)`.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论