Jackson2 将 ObjectNode 保存为字符串
Jackson2 save ObjectNode as String
我有如下字符串 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"
}
]
}
}
}
我想问一下,如何使用 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()
)
}
}
然后打电话给我的主要工作:
val ss = ds.map(s=>s)
ss.print()
但运气不好,总是得到一个空变量。
一种选择是使用 org.json.JSONObject 并执行如下操作:
JSONObject jsonResult = new JSONObject(dataString);
String hugeString = jsonResult.getJSONObject("payload").getString("data");
下面的代码是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);
}
}
我有如下字符串 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"
}
]
}
}
}
我想问一下,如何使用 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()
)
}
}
然后打电话给我的主要工作:
val ss = ds.map(s=>s)
ss.print()
但运气不好,总是得到一个空变量。
一种选择是使用 org.json.JSONObject 并执行如下操作:
JSONObject jsonResult = new JSONObject(dataString);
String hugeString = jsonResult.getJSONObject("payload").getString("data");
下面的代码是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);
}
}