Jackson2保存ObjectNode为字符串



我有字符串json的例子如下:

{
"eventName":"PARTY_COLLECTED",
"payload":{
"vendor":"IBM",
"appVersion":"0.0.1",
"deviceTime":"2021-09-23T04:34:56.086Z",
"app":"ibm",
"platform":"windows",
"data":{
"album":[
{
"hidden":false,
"mediaType":"Image",
"locationLongitude":-1.80317833333334,
"modificationDate":"2015-09-02 20:02:27 +0000",
"creationDate":"2011-03-13 00:17:25 +0000",
"pixelHeight":1010,
"locationLatitude":138.0374445,
"duration":0,
"favorite":false,
"pixelWidth":8588,
"localIdentifier":"104449A1-4G1A-4AB2-B120-B0AD4A8E5555/L0/001"
}
],
"contacs":[

],
"HistoryDevice":[
{
"another_data1":"data1",
"another_data1":"data1",
"another_data1":"data1",
"another_data1":"data1",
"another_data1":"data1"
}
]
}
}
}

我想问,我怎么能得到所有的值在"data"使用Java Jackson2库的字符串?
例如,我有一个变量叫hugeString,所以hugeString的值应该是这样的:

hugeString="{
"album":[
{
"hidden":false,
"mediaType":"Image",
"locationLongitude":-1.80317833333334,
"modificationDate":"2015-09-02 20:02:27 +0000",
"creationDate":"2011-03-13 00:17:25 +0000",
"pixelHeight":1010,
"locationLatitude":138.0374445,
"duration":0,
"favorite":false,
"pixelWidth":8588,
"localIdentifier":"104449A1-4G1A-4AB2-B120-B0AD4A8E5555/L0/001"
}
],
"contacs":[
],
"HistoryDevice":[
{
"another_data1":"data1",
"another_data1":"data1",
"another_data1":"data1",
"another_data1":"data1",
"another_data1":"data1"
}
]
}"

我已经尝试使用下面的代码:

object StreamData {
case class StreamDatas_rows(
data:String
)
def jsonToRow(json_object:ObjectNode):StreamDatas_rows={
def getStringOrNull(jsonNode:JsonNode):String=if(jsonNode==null||jsonNode.isNull) "" else jsonNode.asText()
StreamDatas_rows(
json_object.path("payload").get("data").asText()
)
}
}

,然后调用main job:

val ss = ds.map(s=>s)
ss.print()

但是运气不好,总是得到一个空变量。

下面的代码是Java的实现。

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.LinkedHashMap;
public class SO_Jackson {
private static void parseJson(String jsonData) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(jsonData);
JsonNode node = jsonNode.at("/payload/data");
String subData = objectMapper.writeValueAsString(node);
System.out.println(subData);
}
public static void main(String[] args) throws JsonProcessingException {
String jsonData = "{"eventName":"PARTY_COLLECTED","payload":{"vendor":"IBM","appVersion":"0.0.1","deviceTime":"2021-09-23T04:34:56.086Z","app":"ibm","platform":"windows","data":{"album":[{"hidden":false,"mediaType":"Image","locationLongitude":-1.80317833333334,"modificationDate":"2015-09-02 20:02:27 +0000","creationDate":"2011-03-13 00:17:25 +0000","pixelHeight":1010,"locationLatitude":138.0374445,"duration":0,"favorite":false,"pixelWidth":8588,"localIdentifier":"104449A1-4G1A-4AB2-B120-B0AD4A8E5555/L0/001"}],"contacs":[],"HistoryDevice":[{"another_data1":"data1","another_data1":"data1","another_data1":"data1","another_data1":"data1","another_data1":"data1"}]}}}";
parseJson(jsonData);
}
}

一个选项是使用org.json.JSONObject并做如下操作:

JSONObject jsonResult = new JSONObject(dataString);
String hugeString = jsonResult.getJSONObject("payload").getString("data");

相关内容

  • 没有找到相关文章

最新更新