scala lift环境搭建(9)
来源:互联网 发布:js unicode转中文 编辑:程序博客网 时间:2024/06/01 09:58
You are using Lift 1.0's JsCmd, which produces JSON with single-quoted strings and attempting to parse it with scala's parser, which only supports double-quoted strings.
It is important to realize that there are multiple definitions for JSON.
Are single-quoted strings valid in JSON?
They are according to ECMAScript 5th Ed
They are not according to Crockford's original RFC 4627
Lift and Scala provide many ways to parse JSON, sometimes with differing behavior between versions.
The strings accepted by these parsers are not equivalent.
Here are some comments and examples of the various methods to product and parse JSON strings.
Not Recommended - No longer supported
Scala 2.7.7's parser will not parse single-quoted JSON
This parsing method used in the question
Neutral Recommendation
Lift's util.JSONParser will parse single- or double-quoted JSON strings:
Neutral Recommendation
Lift's json.JsonParser will not parse single-quoted JSON strings:
Not Recommended - output not valid for all JSON parsers
Note the single-quotations around strings:
Neutral Recommendation
Note the double quotations around strings:
It is important to realize that there are multiple definitions for JSON.
Are single-quoted strings valid in JSON?
They are according to ECMAScript 5th Ed
They are not according to Crockford's original RFC 4627
Lift and Scala provide many ways to parse JSON, sometimes with differing behavior between versions.
The strings accepted by these parsers are not equivalent.
Here are some comments and examples of the various methods to product and parse JSON strings.
Producing JSON with the lift-json library DSL
Recommended
Despite its name, this is a separate project with no dependencies on the rest of Lift
example:scala> import net.liftweb.json.JsonASTimport net.liftweb.json.JsonASTscala> import net.liftweb.json.JsonDSL._import net.liftweb.json.JsonDSL._scala> import net.liftweb.json.Printer._import net.liftweb.json.Printer._scala> val json1 = ("foo" -> 4) ~ ("bar" -> "baz")json1: net.liftweb.json.JsonAST.JObject = JObject(List(JField(foo,JInt(4)), JField(bar,JString(baz))))scala> compact(JsonAST.render(json1))res0: String = {"foo":4,"bar":"baz"}scala> val json2 = List(1,2,3)json2: List[Int] = List(1, 2, 3)scala> compact(JsonAST.render(json2))res1: String = [1,2,3]scala> val json3 = ("foo", 4) ~ ("bar", List(1,2,3))json3: net.liftweb.json.JsonAST.JObject = JObject(List(JField(foo,JInt(4)), JField(bar,JArray(List(JInt(1), JInt(2), JInt(3))))))scala> compact(JsonAST.render(json3))res2: String = {"foo":4,"bar":[1,2,3]}
Parsing JSON with the lift-json library
Recommended
Provides implicit mapping to/from scala case-classes
Case-classes defined in the console are not currently supported (will throw a com.thoughtworks.paranamer.ParameterNamesNotFoundException: Unable to get class bytes)
The example below uses PublicID, a pre-existing scala case-class so that it will work on the scala console.
example:scala> import scala.xml.dtd.PublicIDimport scala.xml.dtd.PublicIDscala> import net.liftweb.json._import net.liftweb.json._scala> import net.liftweb.json.JsonAST._import net.liftweb.json.JsonAST._scala> import net.liftweb.json.JsonDSL._import net.liftweb.json.JsonDSL._scala> implicit val formats = DefaultFormats formats: net.liftweb.json.DefaultFormats.type = net.liftweb.json.DefaultFormats$@7fa27eddscala> val jsonAst = ("publicId1" -> "idString") ~ ("systemId" -> "systemIdStr")jsonAst: net.liftweb.json.JsonAST.JObject = JObject(List(JField(publicId,JString(idString)), JField(systemId,JString(systemIdStr))))scala> jsonAst.extract[PublicID]res0: scala.xml.dtd.PublicID = PUBLIC "idString" "systemIdStr"Parsing JSON in scala 2.7.7 and 2.8.1
Not Recommended - No longer supported
Scala 2.7.7's parser will not parse single-quoted JSON
This parsing method used in the question
example:scala>import scala.util.parsing.json.JSON._import scala.util.parsing.json.JSON._scala> parseFull("{\"foo\" : 4 }") res1: Option[Any] = Some(Map(foo -> 4.0))scala> parseFull("[ 1,2,3 ]")res2: Option[Any] = Some(List(1.0, 2.0, 3.0))scala> parseFull("{'foo' : 4 }") res3: Option[Any] = NoneParsing JSON in Lift 2.0 and 2.2 with util.JSONParser
Neutral Recommendation
Lift's util.JSONParser will parse single- or double-quoted JSON strings:
example:scala> import net.liftweb.util.JSONParser._import net.liftweb.util.JSONParser._scala> parse("{\"foo\" : 4 }") res1: net.liftweb.common.Box[Any] = Full(Map(foo -> 4.0))scala> parse("[ 1,2,3 ]")res2: net.liftweb.common.Box[Any] = Full(List(1.0, 2.0, 3.0))scala> parse("{'foo' : 4}") res3: net.liftweb.common.Box[Any] = Full(Map(foo -> 4.0))Parsing JSON in Lift 2.0 and 2.2 with json.JsonParser
Neutral Recommendation
Lift's json.JsonParser will not parse single-quoted JSON strings:
example:scala> import net.liftweb.json._import net.liftweb.json._scala> import net.liftweb.json.JsonParser._import net.liftweb.json.JsonParser._scala> parse("{\"foo\" : 4 }")res1: net.liftweb.json.JsonAST.JValue = JObject(List(JField(foo,JInt(4))))scala> parse("[ 1,2,3 ]")res2: net.liftweb.json.JsonAST.JValue = JArray(List(JInt(1), JInt(2), JInt(3)))scala> parse("{'foo' : 4}") net.liftweb.json.JsonParser$ParseException: unknown token 'Near: {'foo' : 4}at net.liftweb.json.JsonParser$Parser.fail(JsonParser.scala:216)at net.liftweb.json.JsonParser$Parser.nextToken(JsonParser.scala:308)at net.liftweb.json.JsonParser$$anonfun$1.apply(JsonParser.scala:172)at net.liftweb.json.JsonParser$$anonfun$1.apply(JsonParser.scala:129)at net.liftweb.json.JsonParse...Producing JSON with Lift 1.0 JsCmd
Not Recommended - output not valid for all JSON parsers
Note the single-quotations around strings:
example:scala> import net.liftweb.http.js._import net.liftweb.http.js._scala> import net.liftweb.http.js.JE._import net.liftweb.http.js.JE._scala> JsObj(("foo", 4), ("bar", "baz")).toJsCmdres0: String = {'foo': 4, 'bar': 'baz'}scala> JsArray(1,2,3).toJsCmdres1: String = [1, 2, 3]scala> JsObj(("foo", 4), ("bar", JsArray(1,2,3))).toJsCmdres2: String = {'foo': 4, 'bar': [1, 2, 3]}Producing JSON with Lift 2.0 JsCmd
Neutral Recommendation
Note the double quotations around strings:
example:scala> import net.liftweb.http.js._import net.liftweb.http.js._scala> import net.liftweb.http.js.JE._import net.liftweb.http.js.JE._scala> JsObj(("foo", 4), ("bar", "baz")).toJsCmdres0: String = {"foo": 4, "bar": "baz"}scala> JsArray(1,2,3).toJsCmdres1: String = [1, 2, 3]scala> JsObj(("foo", 4), ("bar", JsArray(1,2,3))).toJsCmdres3: String = {"foo": 4, "bar": [1, 2, 3]}
- scala lift环境搭建(9)
- scala lift环境搭建(1)
- scala lift环境搭建(2)
- scala lift环境搭建(3)
- scala lift环境搭建(4)
- scala lift环境搭建(5)
- scala lift环境搭建(6)
- scala lift环境搭建(7)
- scala lift环境搭建(8)
- scala lift环境搭建(10)
- scala lift环境搭建(11)
- scala lift环境搭建(12)
- scala lift环境搭建(12)
- scala lift环境搭建(13)
- Lift第一篇:Lift环境搭建
- scala+lift+maven+eclipse 环境配置
- Scala开发环境搭建
- Scala开发环境搭建
- Jquery Easy UI Databox 用法
- 财务中以dialog的方式配置
- <context-param>与<init-param>的区别与作用含与Spring整合时的文件配置
- java实现的简单词法分析器
- 关于org.hibernate.exception.SQLGrammarException: could not insert:
- scala lift环境搭建(9)
- 动画那点事 UIKit, CoreGraphics
- 关于在 KEIL C51 中嵌入汇编以及C51与A51间的相互调用
- NSURL类
- XML概念与解析库
- Wdp项目总结一-----该慢的时候要慢
- 小型机和PC服务器差异分析
- 多比图形控件开发指南(二)
- 修改线程的属性