通过JSON-lib反序列化字符串值(net.sf.json)

时间:2013-12-03 17:17:36

标签: json deserialization json-lib

我在JSON-Lib中发现了嵌套对象中JSON字符串数据的反序列化问题。以下Groovy代码演示了问题,您可以在Groovy Console中测试它:

@Grab('net.sf.json-lib:json-lib:2.3:jdk15')
import net.sf.json.*

def s = '''\
{
  "attributes": "'{test:1}'",
  "nested": { "attributes": "'{test:1}'" }
}'''

def j = JSONSerializer.toJSON(s)

println j
assert j.attributes == "{test:1}" // OK
assert j.nested.attributes == "{test:1}" // Failed

在上面的示例中,JSON中的attributes具有相同的字符串值"{test:1}"。 JSONSerializer将第一个反序列化为String值,但将第二个反序列化为(可能)JSONObject。我不确定,因为该对象的类返回null。

对于不同的字符串值(看起来不像JSON数据,例如"blah"),它可以正常工作。

如何更改此行为?

更新

我试图引用基于JSON-Lib文档here的字符串值(感谢@GemK指向那里),但结果相同。引用字符串值在嵌套对象中不起作用: - (

0 个答案:

没有答案