在Scala中解析JSON的最直接方法是什么? [英] What is the most straightforward way to parse JSON in Scala?
问题描述
我正在使用Scala开发一个简单的Web应用程序.计划是从外部API获取JSON数据,然后将其插入模板中(不幸的是,以XML格式获取数据不是一种选择).
我已经尝试使用Twitter的scala-json库,但是我无法使其正确编译(github上的代码无法在sbt中更新,说标准项目7.10不可用,而且我没有用那还没出来.)
lift-json看起来令人印象深刻,但似乎比我现在需要的要复杂得多.
尝试导入我在Java中使用过的库jsonic,会导致各种不可思议的错误.这太糟糕了,因为我更喜欢jsonic多么简单.
我使用内置的scala.util.parsing.json.JSON取得了一些进步,但实际上我不知道如何访问元素.您可能已经注意到,我对Scala还是陌生的.您如何访问JSONObjects的属性?
scala .util.parsing.json.JSON 有很多信息,但是是否有关于如何在任何地方使用它的简单教程?
我现在只对将JSON反序列化为Ints,Strings,Maps和Lists感兴趣.我现在不需要序列化对象或使反序列化的对象适合一个类.
任何人都可以指出我使用上述库之一的方法,还是可以帮助我设置可以实现我想要的功能的Java库?
Lift JSON提供了几种不同的反序列化JSON样式.每个人都有其优点和缺点.
val json = JsonParser.parse(""" { "foo": { "bar": 10 }} """)
LINQ样式查询理解:
scala> for { JField("bar", JInt(x)) <- json } yield x
res0: List[BigInt] = List(10)
XPath样式
scala> val JInt(x) = json \ "foo" \ "bar"
x: BigInt = 10
非类型安全值
scala> json.values
res0: Map((foo,Map(bar -> 10)))
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?
Lift JSON provides several different styles of deserializing JSON. Each have their pros and cons.
val json = JsonParser.parse(""" { "foo": { "bar": 10 }} """)
LINQ style query comprehension:
scala> for { JField("bar", JInt(x)) <- json } yield x
res0: List[BigInt] = List(10)
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
implicit val formats = net.liftweb.json.DefaultFormats
case class Foo(foo: Bar)
case class Bar(bar: Int)
json.extract[Foo]
More examples: https://github.com/lift/lift/blob/master/framework/lift-base/lift-json/src/test/scala/net/liftweb/json/ExtractionExamples.scala
XPath style
scala> val JInt(x) = json \ "foo" \ "bar"
x: BigInt = 10
Non-type safe values
scala> json.values
res0: Map((foo,Map(bar -> 10)))
这篇关于在Scala中解析JSON的最直接方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!