我有这个json,我已经解析并用json4s替换字段,json看起来像这样:
{
"id": "6988",
"signatories": [
{
"fields": [
{
"name": "fstname",
"value": "Bruce"
},
{
"name": "sndname",
"value": "Lee"
},
{
"name": "email",
"value": "bruce.lee@company.com"
},
{
"name": "sigco",
"value": "Company"
},
{
"name": "mobile",
"value": "0760000000"
}
]
},
{
"fields": [
{
"name": "fstname",
"value": ""
},
{
"name": "sndname",
"value": ""
},
{
"name": "email",
"value": ""
},
{
"name": "mobile",
"value": ""
},
{
"name": "sigco",
"value": ""
}
]
}
]
}
第二个数组名为" fields",它是我想用实际字符串值替换空字符串的那个。我一直使用json4s transformField函数执行此操作,首先将json解析为JObject,然后使用新值转换JObject。
val a = parse(json)
// transform the second occurance of fields (fields[1])
val v = a.\("signatories").\("fields")(1).transform {
// Each JArray is made of objects. Find fields in the object with key as name and value as fstname
case obj: JObject => obj.findField(_.equals(JField("name", JString("fstname")))) match {
case None => obj //Didn't find the field. Return the same object back to the array
// Found the field. Change the value
case Some(x) =>
obj.transformField { case JField(k, v) if k == "value" => JField(k, JString("New name")) }
}
}
现在我得到了原始解析的json" a",我得到了我的新JArray,其中包含" v"中的更新字段。我需要做的最后一件事是将新值合并到原始的JObject" a"中。我试着替换,没有运气。
val merged = a.replace(List("fields"), v)
问题:
以下是代码的可运行版本,其中包含json的示例打印: http://pastebin.com/e0xmxqFF
答案 0 :(得分:6)
由于您对要更新的字段非常具体,我认为最直接的解决方案是使用mapField
:
val merged = a mapField {
case ("signatories", JArray(arr)) => ("signatories", JArray(arr.updated(1, JObject(JField("fields", v)))))
case other => other
}
replace
无法在我看来使用,因为你需要传递一个索引,这是行不通的,因为replace
期望它的第一个参数是字段列表名称。