无法解析JSON属性"null"; [英] Can't parse JSON property "null"

查看:379
本文介绍了无法解析JSON属性"null";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试解析JSON"null"属性时遇到一个麻烦,请帮助我了解真正的问题.我有以下JSON:

I faced with one trouble when tried to parse JSON "null" property, please help me to understand what's the real problem. I had a following JSON:

{
  "properties" : {        
    "null" : {
      "value" : false
    }
  }
}

我使用了 http://jsonlint.com 来验证此JSON是否有效.我试图从Java解析它:

I used http://jsonlint.com to validate that this JSON is valid. I tried to parse it from java:

import net.sf.json.JSONObject;
import java.io.IOException;

public class Test {
    public static void main(String[] args) throws IOException {
        String st = "{" +
                "      'properties' : {" +
                "        'null' : {" +
                "          'value' : false" +
                "        }" +
                "      }" +
                "}";
        JSONObject.fromObject(st);
    }
}

但有一个例外:

Exception in thread "main" java.lang.ClassCastException: JSON keys must be strings.
    at net.sf.json.JSONObject._fromJSONObject(JSONObject.java:927)
    at net.sf.json.JSONObject.fromObject(JSONObject.java:155)
    at net.sf.json.JSONSerializer.toJSON(JSONSerializer.java:108)
    at net.sf.json.AbstractJSON._processValue(AbstractJSON.java:238)
    at net.sf.json.JSONObject._processValue(JSONObject.java:2655)
    at net.sf.json.JSONObject.processValue(JSONObject.java:2721)
    at net.sf.json.JSONObject.element(JSONObject.java:1786)
    at net.sf.json.JSONObject._fromJSONTokener(JSONObject.java:1036)
    at net.sf.json.JSONObject._fromString(JSONObject.java:1201)
    at net.sf.json.JSONObject.fromObject(JSONObject.java:165)
    at net.sf.json.JSONObject.fromObject(JSONObject.java:134)

我使用了 http://json-lib.sourceforge.net 进行解析.有人可以澄清一下吗?为什么这个库抛出异常,但是在线验证器说它是有效的JSON?这是库中的错误,还是我做错了什么?

I used json-lib-2.4-jdk15.jar from http://json-lib.sourceforge.net to parse it. Could anybody please clarify this? Why this library throws exception, but online validator said that it's valid JSON? It is a bug in the library or I made something wrong?

推荐答案

发布的第一个JSON是有效JSON:但是,Java中的JSON无效-仅[c0>]对于[required]键有效引用.来自 json.org :

The first JSON posted is valid JSON: the JSON in the Java, however, is not valid -- only " is valid for the [required] key quote. From json.org:

字符串是零个或多个Unicode字符的序列,用双引号引起来,使用反斜杠转义....

A string is a sequence of zero or more Unicode characters, wrapped in double quotes, using backslash escapes....

但是,这听起来像个错误,假定并不是由馈给它的 invalid JSON触发的(库可以使用无效的JSON来做任何想要的事情). ..人们将不得不查看源(或错误报告/用户体验),以明确地说这是否确实是错误".我添加了一些建议,可以尝试以下操作,这些建议可以显示预期的行为,也可以更详细地概述原因/问题.

However, that sounds like a bug, assuming it was not triggered by the invalid JSON fed to it (the library can do whatever it wants with invalid JSON)... one would have to look at the source (or bug reports / user experience) to say conclusively if this is indeed a "bug". I have added some suggestions of things to try below which may either show expected behavior or outline the cause/issue in further detail.

考虑这个最小的测试用例(使用有效的JSON):

Consider this minimal test-case (with valid JSON):

String st = "{ \"null\": \"hello world!\" }";

这也可能会引起更多的注意,这取决于提取时第一项是"null"还是null:

This may also shed more light, depending on if the first item is "null" or null when extracted:

String st = "[ \"null\" ]";

快乐的编码.

这篇关于无法解析JSON属性"null";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆