gpt4 book ai didi

scala - 使用反射进行 JSON 反序列化

转载 作者:行者123 更新时间:2023-12-01 11:42:32 25 4
gpt4 key购买 nike

嗨,我正在尝试使用反射提取 JSON

import net.liftweb.json._
case class Bike(make: String, price: Int) {
def this(price: Int) = this("Trek", price)
}

val cls = Class.forName("Bike")
val manifest = Manifest.classType(cls)
val parsedData =net.liftweb.json.JsonParser.parse(json)

JsonParser.parse(""" {"price":350} """).extract[manifest]

但是我收到此错误:
not found: type manifest
JsonParser.parse(""" {"price":350} """).extract[manifest]
^

虽然 list 来自类型 list

最佳答案

可以直接解压成case class

val json = "the json";

val bike = parse(json).extract[Bike];

JSON 解析是通过反射完成的。

如果类是运行时构造,则创建 TypeInfo实例并将其传递给提取方法。

关于scala - 使用反射进行 JSON 反序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18123488/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com