在 Scala 中解析 JSON 最直接的方法是什么?
我正在使用 Scala 开发一个简单的 Web 应用程序。计划是从外部 API 获取 JSON 数据,并将其插入到模板中(不幸的是,不能选择获取 XML 格式的数据)。
我尝试过使用 Twitter 的 scala-json 库,但无法正确编译它(github 上的代码无法在 sbt 中更新,说标准项目 7.10 不可用,我还没有解决这个问题) )。
lift-json 看起来令人印象深刻,但似乎比我现在需要的要复杂得多。
尝试导入我在 Java 中使用过的库 json,会导致各种神秘错误。这太糟糕了,因为我更喜欢 json 的简单性。
我在内置的 scala.util.parsing.json.JSON 方面取得了一些进展,但实际上我不知道如何访问这些元素。正如您可能已经注意到的,我对 Scala 有点陌生。如何访问 JSONObject 的属性?
scala.util。 parsing.json.JSON 有很多信息,但是有关于如何在任何地方使用它的简单教程吗?
我目前只对将 JSON 反序列化为整数、字符串、映射和列表感兴趣。我目前不需要序列化对象或使反序列化的对象适合一个类。
任何人都可以向我指出使用上述库之一的方法,或者帮助我设置一个可以执行我想要的操作的 Java 库吗?
I'm working on a simple web application with Scala. The plan is to obtain JSON data from an external API, and insert it into a template (unfortunately, obtaining the data in XML is not an option).
I've tried working with Twitter's scala-json library, but I can't get it to compile properly (the code on github fails to update in sbt, saying standard-project 7.10 is not available and I haven't worked that out yet).
lift-json looks impressive, but appears to be a lot more elaborate than I need right now.
Trying to import a library I've worked with in Java, jsonic, results in various arcane errors. This is too bad because I rather like how straightforward jsonic is.
I've made a bit of progress with the built in scala.util.parsing.json.JSON, but actually I can't tell how to access the elements. I'm somewhat new to Scala, as you may have noted. How do you access the properties of JSONObjects?
scala.util.parsing.json.JSON has a lot of information, but is there a straightforward tutorial on how to use this anywhere?
I'm really only interested in deserializing JSON at the moment, to Ints, Strings, Maps and Lists. I don't have a need to serialize objects or make the deserialized objects fit into a class at the moment.
Can anyone point me to ways to work with one of the aforementioned libraries, or help me get set up with a Java lib that will do what I want?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Lift JSON 提供了几种不同样式的反序列化 JSON。每个都有其优点和缺点。
LINQ 风格查询理解:
更多示例:
http://github.com/lift/lift/blob/master/framework/lift-base/lift-json/src/test/scala/net/liftweb/json/QueryExamples.scala
使用案例类提取值
更多示例:https://github.com/lift/lift/blob/master/framework/lift-base/lift-json/src/test/scala/net/ liftweb/json/ExtractionExamples.scala
XPath 样式
非类型安全值
Lift JSON provides several different styles of deserializing JSON. Each have their pros and cons.
LINQ style query comprehension:
More examples:
http://github.com/lift/lift/blob/master/framework/lift-base/lift-json/src/test/scala/net/liftweb/json/QueryExamples.scala
Extract values with case classes
More examples: https://github.com/lift/lift/blob/master/framework/lift-base/lift-json/src/test/scala/net/liftweb/json/ExtractionExamples.scala
XPath style
Non-type safe values