2

I have a JSON response from a server. It looks somehow like this:

"json_key" : { 
     "key_1" : value,
     "key_2" : json_object, 
     "key_3" : json_object
}

The problem is that one of the json objects comes in a specific encoding

"object_key": "a:4:{s:6:\"ITEM_KEY_1\";s:2:\"ITEM_VALUE_1\";s:6:\"ITEM_KEY_2\";s:2:\"ITEM_VALUE+2\";s:8:\"ITEM_KEY_3\";s:8:\"ITEM_VALUE_3\";s:8:\"ITEM_KEY_4\";s:5:\"ITEM_VALUE_4\";}"

As I understand it's an json_encoded string. How can I convert it to a java JSONObject? Asking this because when I try the usual way JSONObject json = new JSONObject(jsonStr); it throws a JSONException - cant convert String to JSONObject.

Edit Every validation tool see the objects value as a string, not a JSON.

5
  • Is this the exact result or not? Commented Jan 8, 2013 at 14:23
  • 3
    a:4:{s:6:\"ITEM_KEY_1\"... is not a json encoded. It's some other encoding format. Commented Jan 8, 2013 at 14:24
  • 1
    I suppose it cames from PHP serialize() Commented Jan 8, 2013 at 14:24
  • Have you tried JSONArray? Check your json is valid or not at json.parser.online.fr Commented Jan 8, 2013 at 14:26
  • [Loris] - yes... its a part of the exact result. I only changed the keys and values, because they contain some private data. Commented Jan 8, 2013 at 14:43

2 Answers 2

1

The problem is that
"object_key": "a:4: s:6:\"ITEM_KEY_1\";s:2:\"ITEM_VALUE_1\";s:6:\"ITEM_KEY_2\";s:2:\"ITEM_VALUE+2\";s:8:\"ITEM_KEY_3\";s:8:\"ITEM_VALUE_3\";s:8:\"ITEM_KEY_4\";s:5:\"ITEM_VALUE_4\";}"

isn't valid JSON so it's going to error when you try and parse it. The first error in the string comes with: "object_key": "a: 4:, but I'm guessing that you want the whole string to come under object_key

AFAIK, JSON can't ignore ':' inside a value, and there's not an escape for it. If the idea isn't that the object key contains all of that, then the JSON needs restructuring until it's valid.

As a tool, use http://jsonlint.com/, which allows you to check that JSON is valid

RE: Yami's edit - I'm afraid that the JSON sample that you gave to us is invalid, as shown by the website I listed. For more help on the matter, you need to change the private JSON data to something which produces the same result as what you're experiencing when the JSON is validated by one of these sites, otherwise there's not a lot we can do to help try and validate it

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

Comments

1

Found a nice solution using the serialized-php-parser lib:

JSONObject jsonObject = new JSONObject(response);
String theNonReadableJson = jsonObject.getString("key");

SerializedPhpParser serializedPhpParser = new SerializedPhpParser(
                        theNonReadableJson);
Object serializedObject = serializedPhpParser.parse();

JSONObject readableJson = new JSONObject(serializedObject.toString());

After the serialization the JSON object can be parset like a charm.

I would like to thank Loris, for giving me the idea of an PHP serialize() result, and Matt Taylor for trying to help me solve this problem.

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.