-1

I create some JSONObject using java classes. Lets call it MyJSONObject. It should be the inner String part of another JSON object. Like that:

[
    {
        "httpRequest": {...},
        "httpResponse": {
            "headers": {
                "content-type": [
                    "application/json"
                ]
            },
            "body": "MyJSONObject"
        }
]

The problem is when i try to put it as string using mapper.writeValueAsString(MyJSONObject) I receive all the quotes with backslash in console body :

[
    {
        "httpRequest": {...},
        "httpResponse": {
            "headers": {...},
            "body": "{\"key\":\"value\",\"key\":\"value\"}"
        }
]

instead of expected:

        "body": "{"key":"value","key":"value"}"

I tried to replace each quote by quote with backslash, using method replace("\"", "\\\"") to my string, but the result is even worse:

        "body": "{\\\"key\\\":\\\"value\\\",\\\"key":\\\"value\\\"}"

How can i just put my string avoid this backslashes?

javagirl
  • 21
  • 4

1 Answers1

-1

You can try with below approach which will give you expected result.

JSONObject subJson = new JSONObject(); subJson.put(“name”, “java”);

JSONObject parentJson = new JSONObject() parentJson.put(“company”, ”stack”); parentJson.put(“employees”, subJson);

System.out.print(“Final output - ” + parentJson.toString());

Abhishek Kumar
  • 435
  • 1
  • 6
  • 16