如何使用Json4s在案例类中表示嵌套的JSON对象?

编程入门 行业动态 更新时间:2024-10-16 22:21:29
本文介绍了如何使用Json4s在案例类中表示嵌套的JSON对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

给出一个带有嵌套对象的JSON对象,该对象的属性是未知的:

Given an example JSON object with a nested object who's properties are unkown:

{ "Key":"01234", "eventProperties":{ "unknownProperty1":"value", "unknownProperty2":"value", "unknownProperty3":"value" }, }

我试图将json4s的提取函数与以下案例类一起使用(In Scala):

I have tried to use json4s' extract function with the following case class (In Scala):

case class nestedClass(Key:String, eventProperties:Map[(String,Any)])

导致以下错误:

org.json4s.package$MappingException: Can't find constructor for nestedClass

是否可以在不定义eventProperties的每个可能属性的情况下执行此操作?

Is it possible to do this without defining every possible property of eventProperties?

更新: json4s 3.2.10中存在导致此问题的错误 - 更新到3.2.11并解压缩到Map [String ,任何]工作正常。

Update: there was a bug in json4s 3.2.10 causing this issue - updating to 3.2.11 and extracting to Map[String,Any] works fine.

推荐答案

我不知道你在做什么来获得你发布的例外,但是以下作品(注意地图而不是列表):

I'm not sure what you are doing to get the exception you have posted, but the following works (note a Map instead of List):

import org.json4s._ import org.json4s.jackson.JsonMethods._ import org.json4s.DefaultFormats val json = parse(""" { "key":"01234", "eventProperties":{ "unknownProperty1":"value", "unknownProperty2":"value", "unknownProperty3":"value" } } """) case class NestedClass(key:String, eventProperties:Map[String,Any]) implicit val formats = DefaultFormats json.extract[NestedClass]

更多推荐

如何使用Json4s在案例类中表示嵌套的JSON对象?

本文发布于:2023-07-14 14:45:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1105100.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   如何使用   类中   对象   案例

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!