jackson 读取带有 $ 符号的节点字段
jackson read node field with $ symbol
API 的回复看起来像
"node1": [
{
"$field": "https://somethingh.com",
"type": "object",
"meta:Type": "object"
},
{
"$field": "https://somethingelse.com",
"type": "object",
"meta:Type": "object"
}
],
key检索节点时,有时是检索,有时是objNode.get("$field") returns null。所以得到空指针异常。
我有什么方法可以避免它。我不想使用 get(0) (元素的索引)。因为后面你的位置可能会改变..
val mapper = new ObjectMapper()
val node = mapper.readTree(res)
val nodelist = node.get("node1")
if (nodelist != null) {
for (objNode <- nodelist) {
println(s"objNode => $objNode and value => ${objNode.get("$field")} ")
val fieldColumn = objNode.get("$field").textValue()
}
问题是 $ 是一个特殊字符。代码按原样工作。
API 的回复看起来像
"node1": [
{
"$field": "https://somethingh.com",
"type": "object",
"meta:Type": "object"
},
{
"$field": "https://somethingelse.com",
"type": "object",
"meta:Type": "object"
}
],
key检索节点时,有时是检索,有时是objNode.get("$field") returns null。所以得到空指针异常。
我有什么方法可以避免它。我不想使用 get(0) (元素的索引)。因为后面你的位置可能会改变..
val mapper = new ObjectMapper()
val node = mapper.readTree(res)
val nodelist = node.get("node1")
if (nodelist != null) {
for (objNode <- nodelist) {
println(s"objNode => $objNode and value => ${objNode.get("$field")} ")
val fieldColumn = objNode.get("$field").textValue()
}
问题是 $ 是一个特殊字符。代码按原样工作。