3

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
    }
  }
}

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);
    }
}

But got the exception:

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)

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?

erkfel
  • 1,588
  • 2
  • 17
  • 29
  • This isn't the cause of the problem, but `null` is a keyword in JavaScript. Do you really need to use it as the name of your property? – aroth Jul 14 '11 at 00:37
  • @aroth 1) JSON is not JavaScript -- it is a formalized subset of JavaScript literals where object key are required to be surrounding in double-quotes 2) `"null"` is not `null` –  Jul 14 '11 at 00:41
  • @pst - JSON is not JavaScript, but often you end up working with it in JavaScript (not that this won't work in JavaScript, but it certainly makes for some [confusing looking code](http://jsfiddle.net/SZSm6/)). And `null` is also a keyword in Java. That `"null"` is not `null` is immaterial. It is still semantically vague to call a property `"null"` in my opinion. – aroth Jul 14 '11 at 00:48

4 Answers4

3

JSON-lib initially parses and populates a Java Map with the input JSON. Unfortunately, JSON-lib then checks whether every JSON object element name is a JSON null. It's null check is performed in the JSONNull.equals(Object) method. This method returns true for a "null" JSON string, which of course is not actually a JSON null value.

I recommend filing a bug with the JSON-lib project for this issue. The implementation of JSONNull.equals(Object) is flawed.

Unfortunately, it's not possible to handle this with a custom PropertyNameProcessor.

Options available for a more immediate solution include altering the JSON-lib code yourself, or switching libraries.

If you can switch libraries, I highly recommend Jackson. Following is an example of using it to deserialize the example JSON in the original question.

/*
{
  "properties" : {        
    "null" : {
      "value" : false
    }
  }
}
 */
String json = "{\"properties\":{\"null\":{\"value\":false}}}";

ObjectMapper mapper = new ObjectMapper();
Map<String, Object> map = mapper.readValue(json, Map.class);
System.out.println(map);
// output: {properties={null={value=false}}}

Map<String, Object> propertiesMap = (Map) map.get("properties");
System.out.println(propertiesMap);
// output: {null={value=false}}

Map<String, Object> nullMap = (Map) propertiesMap.get("null");
System.out.println(nullMap);
// output: {value=false}
Programmer Bruce
  • 64,977
  • 7
  • 99
  • 97
2

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:

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

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.

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

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

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

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

Happy coding.

0

The gson library link is:

http://code.google.com/p/google-gson/

I normally usr gson to generate the josn string,so I found some example someone else posted in stackoverflow to parse json string with gson,see the link:

Converting JSON to Java

Community
  • 1
  • 1
jack jin
  • 1,254
  • 10
  • 12
-1

suggest you to use Gson,

and construct the json string using java Map and List, then use Gson to output the Map or List object

jack jin
  • 1,254
  • 10
  • 12