在 Lift Scala 中将 JValue 转换为字符串

Convert JValue to String in Lift Scala

我有 json 个字符串。我使用 net.liftweb.JsonParser

将其转换为 JValue
val x : JValue =  parse(json)

然后我使用 replace()

修改了一个名为 "name" 的字段的值
x.replace("name" :: Nil, JString("Tim"))

问题是如何将此 JValue 转换回 json 字符串

你可以简单地使用这个

import net.liftweb.json._
compact(render(x))

这将为您提供一个 json 这种形式的 JValue 对象的字符串版本

String = {"name":"Tim"}

在 2018 年的最新版本 3.3.0 中,使用以下内容将 JsonAST.JValue 转换为 json 字符串:

import net.liftweb.json._
compactRender(jValue)