5

I have the following String :

{
    "response": true,
    "model_original_id": "5acea0b5:1431fde5d6e:-7fff",
    "model_new_id": 500568,
    "model_new_version": 1,
    "reload": true,
    "idsModelProperties": [{
        "key": "creation_date",
        "value": "2013-12-23"
    },
    {
        "key": "state",
        "value": 1,
        "displayValue": "Analisi"
    }],
    "idsNodes": [],
    "idsConnectors": [],
    "idsNodesProperties": []
}

and i need to parse it as a JSONObject. I tried to use quickjson but it gives me an exception when it tries to parse an emty string . This is what i tried :

JsonParserFactory factory=JsonParserFactory.getInstance();
JSONParser parser=factory.newJsonParser();
Map jsonData=parser.parseJson(response_output);

Exception: Exception in thread "main" com.json.exceptions.JSONParsingException: @Key-Hierarchy::root/idsNodes[0]/ @Key:: Value is expected but found empty...@Position::256

Any idea?

6
  • And what empty string? Commented Dec 23, 2013 at 15:30
  • Is the string empty or null ? If it fails on "empty" string only parse if non-empty string is given. Commented Dec 23, 2013 at 15:32
  • 1
    It looks like quickjson cannot handle empty json arrays. Try using a different parsing library. Commented Dec 23, 2013 at 15:36
  • code.google.com/p/quick-json/issues/detail?id=4 Commented Dec 23, 2013 at 15:37
  • Any idea which library should I use? Commented Dec 23, 2013 at 15:40

2 Answers 2

3

I'll give you an alternative since it looks like quick-json has trouble parsing empty json arrays. Check out Gson.

String json = "{ \"response\": true, \"model_original_id\": \"5acea0b5:1431fde5d6e:-7fff\", \"model_new_id\": 500568, \"model_new_version\": 1, \"reload\": true, \"idsModelProperties\": [{ \"key\": \"creation_date\", \"value\": \"2013-12-23\" }, { \"key\": \"state\", \"value\": 1, \"displayValue\": \"Analisi\" }], \"idsNodes\": [], \"idsConnectors\": [], \"idsNodesProperties\": []}";
JsonParser jsonParser = new JsonParser();
JsonElement jsonElement = jsonParser.parse(json);

JsonElement is an abstract class. Its sub types are JsonArray, JsonNull, JsonObject and JsonPrimitive. In the example above, the actual instance is a JsonObject because your json String is a json object. It internally contains a LinkedTreeMap but you don't really need access to it. You can access the different json objects directly on the JsonElement.

Sign up to request clarification or add additional context in comments.

2 Comments

In fact I get the json string dinamically... So I cannot do escape of " manually ...
@user3107262 That's just an example to make it run. You don't need to escape anything if it's already in the String. Escaping is just for String literals.
2

While the author of the question has already accepted an answer, someone else might be searching for the same issue. I've fixed the issues I know of with quick-json in https://code.google.com/p/quick-json/issues/detail?id=11

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.