0

Below is some sample code:

import org.json.JSONObject;

String k = "{\"root1\":\"{\\\"key1\\\":\\\"val1\\\",\\\"key2\\\":1,\\\"key3\\\":null}\",\"root2\":\"OTHERS\",\"root3\":1}";
JSONObject obj = new JSONObject(str);
System.out.println(obj);

/* 
here I want to do something like:
JSONObject innerObj = (JSONObject) obj.get("root1");
String k1 = innerObj.get("key1");

Also, should work with nested inner objects, so for example, should be able to do:
String k4 = innerObj.get("key1.innerKey1");
*/
{"root1":"{\"key1\":\"val1\",\"key2\":1,\"key3\":null}","root2":"OTHERS","root3":1}

On doing JSONObject innerObj = (JSONObject) obj.get("root1"); - it gives:

Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to org.json.JSONObject

I tried Gson, JSONParser - but still unable to do it ...

Note: Parsing of string 'k' works fine, as the obj is populated correctly and is printed out. It fails when I try to access objects within this obj: JSONObject innerObj = (JSONObject) obj.get("root1");

3
  • 1
    The root1 key maps to a string. That's what the \" quotes are doing inside your JSON. It looks like your JSON was put together wrong. Commented Nov 2, 2020 at 19:35
  • Your root1 property is not a JSON object; it is a string whose value is JSON, whose root value is a JSON object. Correct the assembly if at all possible; if not, you'll need to extract the string and run it through parsing a second time. Commented Nov 2, 2020 at 19:48
  • @chrylis -cautiouslyoptimistic- - hw to do that ? i am using import org.json.JSONObject; Commented Nov 2, 2020 at 21:54

2 Answers 2

1

There is utility StringEscapeUtils class in apache commons text library for such translations. Applying it will remove those extra double quotes around root1 value.

Add this commons-text jar dependency to your project.

Usage -

String k = "{\"root1\":\"{\\\"key1\\\":\\\"val1\\\",\\\"key2\\\":1,\\\"key3\\\":null}\",\"root2\":\"OTHERS\",\"root3\":1}";

// add this line
String unescapedJsonString = StringEscapeUtils.unescapeJson(k);

 // pass new json string to Json library
JSONObject obj = new JSONObject(unescapedJsonString);
Sign up to request clarification or add additional context in comments.

Comments

0

Please check this-

public static void main(String[] args) {

    String k ="{\"root1\":{\"key1\":\"val1\",\"key2\":1,\"key3\":null},\"root2\":\"OTHERS\",\"root3\":1}";
    JSONObject obj = new JSONObject(k);
    System.out.println(obj);
     
    //here I want to do something like:
    JSONObject innerObj = (JSONObject) obj.get("root1");
    Object k1 =  innerObj.get("key1");
    System.out.println(k1);
    Object k2 =  innerObj.get("key2");
    System.out.println(k2);
    Object k3 =  innerObj.get("key3");
    System.out.println(k3);
    
    System.out.println(obj.get("root2"));
    System.out.println(obj.get("root3"));
}

Output -

{"root1":{"key1":"val1","key2":1,"key3":null},"root2":"OTHERS","root3":1}
val1
1
null
OTHERS
1

2 Comments

not sure who down voted it ... i did similar thing in my actual code, but when i access inner element and then print it, it gives something like: ``` JSONObject innerObj = (JSONObject) obj.get("root1"); logger.debug("inner obj: " + innerObj.toString()); ``` inner obj: {"bytes":[123,34,101],"empty":false}
and my problem is as stated by @chrylis -cautiouslyoptimistic- above : 'Your root1 property is not a JSON object; it is a string whose value is JSON, whose root value is a JSON object. Correct the assembly if at all possible; if not, you'll need to extract the string and run it through parsing a second time.' yes, so value of my root1 is a string with JSONObj - not a JSONObj

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.