考虑以下json:
{
"type":"A1",
"system":{
"path":"/example.org/FooBar",
"lastModified":"2013-10-01T12:00:00Z"
},
"fields":{
"foo1":["bar1"],
"foo2":["bar2"],
"foo3":["bar3"]
}
}
现在,使用lift-json,我想把这个json改成:
{
"type":"A1",
"system":{
"path":"/example.org/FooBar",
"lastModified":"2013-10-01T12:00:00Z"
},
"fields":{
"foo1":["bar1"],
"foo2":["bar2"],
"foo3":["bar3"]
},
"injected":{
"bar1":"foo1",
"bar2":"foo2"
}
}
所以,我尝试了以下方法:
scala> val json = parse("""
|{
| "type":"A1",
| "system":{
| "path":"/example.org/FooBar",
| "lastModified":"2013-10-01T12:00:00Z"
| },
| "fields":{
| "foo1":["bar1"],
| "foo2":["bar2"],
| "foo3":["bar3"]
| }
|}""")
json: net.liftweb.json.JValue = JObject(List(JField(type,JString(A1)), JField(system,JObject(List(JField(path,JString(/example.org/FooBar)), JField(lastModified,JString(2013-10-01T12:00:00Z))))), JField(fields,JObject(List(JField(foo1,JArray(List(JString(bar1)))), JField(foo2,JArray(List(JString(bar2)))), JField(foo3,JArray(List(JString(bar3)))))))))
scala> json transform{case JObject(l) => JObject(l ::: List(JField("injected", ("bar1" -> "foo1") ~ ("bar2" -> "foo2"))))}
res0: net.liftweb.json.JsonAST.JValue = JObject(List(JField(type,JString(A1)), JField(system,JObject(List(JField(path,JString(/example.org/FooBar)), JField(lastModified,JString(2013-10-01T12:00:00Z)), JField(injected,JObject(List(JField(bar1,JString(foo1)), JField(bar2,JString(foo2)))))))), JField(fields,JObject(List(JField(foo1,JArray(List(JString(bar1)))), JField(foo2,JArray(List(JString(bar2)))), JField(foo3,JArray(List(JString(bar3)))), JField(injected,JObject(List(JField(bar1,JString(foo1)), JField(bar2,JString(foo2)))))))), JField(injected,JObject(List(JField(bar1,JString(foo1)), JField(bar2,JString(foo2)))))))
scala> Printer.pretty(render(res0))
res1: String =
{
"type":"A1",
"system":{
"path":"/example.org/FooBar",
"lastModified":"2013-10-01T12:00:00Z",
"injected":{
"bar1":"foo1",
"bar2":"foo2"
}
},
"fields":{
"foo1":["bar1"],
"foo2":["bar2"],
"foo3":["bar3"],
"injected":{
"bar1":"foo1",
"bar2":"foo2"
}
},
"injected":{
"bar1":"foo1",
"bar2":"foo2"
}
}
如您所见,该injected
部分也被添加到fields
&system
中。我只是想在根目录下添加一次。
那么,我做错了什么?以及如何将 json 转换为我需要的正确结构?