问题
I'm using scala / lift for a Rest API. Under the hood I'm generating xml which will be converted to json via Xml.toJson() as output.
Now i noticed some strange behavior which drives me nuts.
for example i have the following xml:
<data>
<item>
<foo>1</foo>
<bar>1</bar>
</item>
<item>
<foo>2</foo>
<bar>2</bar>
</item>
</data>
the result of xml.toJson() looks like:
JObject(
List(
JField(
data,JObject(
List(
JField(item,
JArray(
List(
JObject(
List(
JField(foo,JString(1)),
JField(bar,JString(1)))
),
JObject(
List(
JField(foo,JString(2)),
JField(bar,JString(2))
)
)
)
)
)
)
)
)
)
)
but if i add a new xml element:
<data>
<baz>234</baz>
<item>
<foo>1</foo>
<bar>1</bar>
</item>
<item>
<foo>2</foo>
<bar>2</bar>
</item>
</data>
the result is different regarding the JArray:
JObject(
List(
JField(data,JObject(
List(
JField(baz,JString(234)),
JField(item,JObject(
List(
JField(foo,JString(1)),
JField(bar,JString(1))
))
),
JField(item,JObject(
List(
JField(foo,JString(2)),
JField(bar,JString(2))
))
)
)
))
)
)
The array isn't defined and i have two objects with the name "item". Is this a normal behavior? I would like to have the array without wrapping someting aroung the "item" Tags.
回答1:
Yep, this is the intended behavior: net.liftweb.json.Xml
will only group child elements into a JArray
if they all have the same name. You can try to get around this behavior by manipulating the generated JSON:
JObject(
(json \ "data").asInstanceOf[JObject].obj.groupBy(_.name).map {
case (_, v :: Nil) => v
case (k, vs) => JField(k, JArray(vs.map(_.value)))
}.toList
)
But there are at least a couple of potential problems here:
- We're using
groupBy
, so we may end up rearranging the order of child elements. - If there's only one
item
, it won't get wrapped in aJArray
.
Depending on how much you care, you could write your way around these issues, but it's almost certainly not worth it. Just ignore net.liftweb.json.Xml
and generate both your XML and your JSON from a Scala data structure.
来源:https://stackoverflow.com/questions/12019633/converting-xml-to-json-with-lift-behaves-strange