[英]Parse JSON with JsonArray and HashMap
我需要解析以下 JSON 并将其中的值添加到三个不同的 Java 对象中。 为了做到这一点,我正在考虑组建其他 3 个 Json。 我有一些解析问题,因为 JSON 有点复杂。 JSON 如下:
{
"totalCount": 1,
"results": [
{
"teleCommunications": [
{
"areaCode": "100",
"telephoneNumber": "300-2444",
"internationalAreaCode": "",
"communicationType": 1
},
{
"areaCode": "100",
"telephoneNumber": "200-2555",
"internationalAreaCode": "",
"communicationType": 5
}
],
"delegate": {
"id": 0,
"range": 0,
},
"name": "Andrew",
"composedKey": {
"id": 615,
"range": 50,
},
"isBranchAddress": false,
"emailAddresses": [
{
"emailAddressType": 9,
"emailAddress": "andrew.brown@gmail.com"
}
],
"name": "Brown",
"zipCodeCity": "65760 Leipzig",
"salutation": "Mr.",
"openingDate": "2019-09-20",
"streetHouseNumber": "Offenbach. 37",
"modificationTimestamp": "2018-01-27"
}
]
}
我需要分别从 JSON (或任何其他方式)中的 name、zipCodeCity、salutation、openingDate、streetHouseNumber、不同 JSON 中的 emailAddresses 和另一个中的其他数据中获取值。
我试过这段代码:
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
HashMap<String, Object> result = new ObjectMapper().readValue(response, HashMap.class);
Object object = result.get("results");
List<HashMap<String, String>> jsonValues = (List<HashMap<String, String>>)object;
for(String key : jsonValues.get(0).keySet()){
System.out.println("name value " + jsonValues.get(0).get("name"));
System.out.println("zip code City " + jsonValues.get(0).get("zipCodeCity"));
}
问题是我不会 go 对于这样的硬编码方式......它不是很合适。 有谁知道更好的方法来存储我需要的值并更优化地解析 Json?
谢谢
寻找这个链接,有一个专门为你准备的演示应用程序: https://github.com/nalmelune/jackson-demo-58591850 (看看JacksonTest
)
首先,您需要为此(或所谓的dto
)表示结构的数据类。 您可以为此使用在线生成器(谷歌“json to java dto online”)或自己做。 例如,这里是根 object(有关更多信息,请参见 github 链接):
public class Root {
private int totalCount;
private List<Results> results;
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getTotalCount() {
return this.totalCount;
}
public void setResults(List<Results> results) {
this.results = results;
}
public List<Results> getResults() {
return this.results;
}
}
然后配置您的 ObjectMapper:
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
并使用它(不要创建新的映射器,就像在您的示例中一样):
Root result = mapper.readValue(response, Root.class);
最后,可以像往常一样访问Plain Old Java 对象:
for (Results resultItem : result.getResults()) {
System.out.println(resultItem.getSalutation());
System.out.println(resultItem.getName());
System.out.println(resultItem.getZipCodeCity());
System.out.println(resultItem.getOpeningDate());
System.out.println(resultItem.getStreetHouseNumber());
}
为了使其正常工作,请务必验证您的 json (“范围”后有无效逗号,“名称”出现两次,依此类推,已修复在 github 上)。 并确保通过添加com.fasterxml.jackson.datatype:jackson-datatype-jsr310
模块在类路径中包含jsr310
。 对于LocalDate
和LocalDateTime
对象,您将需要它。
您可以创建 java class 如下所示
@Data
class MyCustomClass{
int totalCount;
List<TeleCommunicationDetail> results;
// other props
}
接下来,您可以向 TeleCommunicationDetail 添加属性,最后您可以使用MyCustomClass reaonseInJavaObject = objectMapper.readValue(myJson,MyCustomClass.class);
有关详细信息,请参阅此问题。
Your approach is correct, as you simply read your JSON object as Map<String, Object>
which always will work (as long as JSON Object is valid). 然后你检索你期望在那里的价值观。 另一种方法是创建一个 Class 映射到您的原始 JSON 并使用ObjectMapper
将 JSON 解析为您的 ZA2F2ED4F8EBC2ABCBBDC4。 然后,您可以使用 class 设置器和获取器检索数据。 在这种情况下,您不需要使用“结果”等关键字符串,但您必须确保您的 JSON 不仅是有效的 JSON,而且始终符合您的 ZA2F2ED4F8EBC2CBB4C21A29DC40。 选择您的选项...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.