带有列表的字符串的JSONObject到JsonNode

问题描述 投票:1回答:2

我将JSONObject转换为字符串,以便在杰克逊中与杰克逊解析,但我的JSONObject中有一个列表,当我用ObjectMapper解析时,我得到了这个:

["{Property1 : value1, Property2 : value2}"]

而且我不能打电话给myJsonNodeObject.get(i).get("Property1"),这是我的问题。

我试图将我的列表强制转换为JSONObject中的JSONArray,但不起作用。

resultAsJSONObject = new JSONObject();
resultAsJSONObject.put("Label", getMetricStatisticsResult.getLabel());
resultAsJSONObject.put("Datapoints", getMetricStatisticsResult.getDatapoints());
resultAsJSONObject.put("fromDate", fromDate.getTimeInMillis());
resultAsJSONObject.put("toDate", toDate.getTimeInMillis());
resultAsJSONObject.put("error", "");
resultAsString = resultAsJSONObject.toString();

mapper.readValue(resultAsString, MetricsData.class);
java jackson jsonobject
2个回答
2
投票
假设您有一个只想更改的JSON字符串。然后,您可以使用Jackson将其解析为ObjecNode,然后进行修改。这是一个例子:

0
投票
此方法非常简单,也可以使用
© www.soinside.com 2019 - 2024. All rights reserved.