Convert JSON to Java POJO

huangapple go评论88阅读模式
英文:

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.

			{
			&quot;uniqueID1&quot;: {
				&quot;var1&quot;: true,
				&quot;var2&quot;: 0.0,
				&quot;var3&quot;: &quot;cash&quot;,
				&quot;vars&quot;: {
					&quot;0&quot;: {
						&quot;varsvar1&quot;: &quot;cash&quot;,
						&quot;varsvar2&quot;: {
							&quot;0&quot;: {
								&quot;varsvarvar1&quot;: &quot;cash&quot;
							},
							&quot;1&quot;: {
								&quot;varsvarvar1&quot;: &quot;cheque&quot;
							}
						}
					},
					&quot;1&quot;: {
						&quot;varsvar1&quot;: &quot;cash&quot;,
						&quot;varsvar2&quot;: {
							&quot;0&quot;: {
								&quot;varsvarvar1&quot;: &quot;cash&quot;
							},
							&quot;1&quot;: {
								&quot;varsvarvar1&quot;: &quot;cheque&quot;
							}
						}
					}
				}
			},
			&quot;uniqueID2&quot;: {
				&quot;var1&quot;: true,
				&quot;var2&quot;: 0.0,
				&quot;var3&quot;: &quot;cash&quot;,
				&quot;vars&quot;: {
					&quot;0&quot;: {
						&quot;varsvar1&quot;: &quot;cash&quot;,
						&quot;varsvar2&quot;: {
							&quot;0&quot;: {
								&quot;varsvarvar1&quot;: &quot;cash&quot;
							},
							&quot;1&quot;: {
								&quot;varsvarvar1&quot;: &quot;cheque&quot;
							}
						}
					},
					&quot;1&quot;: {
						&quot;varsvar1&quot;: &quot;cash&quot;,
						&quot;varsvar2&quot;: {
							&quot;0&quot;: {
								&quot;varsvarvar1&quot;: &quot;cash&quot;
							},
							&quot;1&quot;: {
								&quot;varsvarvar1&quot;: &quot;cheque&quot;
							}
						}
					}
				}
			},
			.... 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&lt;String,Pojo3&gt; vars;
		}
		
		public class Pojo3{
			private String uniqueIDPojo3;
			private Pojo4 pojo4;
		}
		
		public class Pojo4{
			private String varsvar1;
			Map&lt;String,Pojo5&gt; 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&lt;String,Pojo2&gt; pojos = new ObjectMapper().readValue(src, new TypeReference&lt;HashMap&lt;String,Pojo2&gt;&gt;() {});

答案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>



huangapple
  • 本文由 发表于 2020年7月28日 19:38:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63133248.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定