Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java JSON backslash added to JSON response

Why these backslash is coming and how to remove them while browser displays the json data to the client?

The o/p json response seems to be valid if those backslash(es) were not present

testbookdata.xml

<Users>
    <User>
        <Name>Unni</Name>
        <Books>
            <Book>book1</Book>
            <Book>book2</Book>
            <Book>book3</Book>
        </Books>
    </User>
    <User>
        <Name>Ammu</Name>
        <Books>
            <Book>book1</Book>
            <Book>book2</Book>
            <Book>book4</Book>
        </Books>
    </User>
</Users>

This xml is converted to a JSONObject thru org.json library

org.json.JSONObject xmlJSONObj = XML.toJSONObject(booksXMLString);

Finally I have a class that tells what all to be converted to JSON on a particular user request,

A property of the class:

@JsonInclude(Include.NON_NULL)
@JsonProperty(value = "jsondata")
public String getJson() {
    return json.toString();
}

If I just print the data,

json data : ...
{"Users":{"User":[{"Name":"Unni","Books":{"Book":["book1","book2","book3"]}},{"Name":"Ammu","Books":{"Book":["book1","book2","book4"]}}]}}

Finally the webservice controller method which has a public @ResponseBody annotation calls the service and returns the object that has the @Json annotations

Problem:

When the result is displayed by the browser, it is like this -

"jsondata": "{\"Users\":{\"User\":[{\"Name\":\"Unni\",\"Books\":{\"Book\":[\"book1\",\"book2\",\"book3\"]}},{\"Name\":\"Ammu\",\"Books\":{\"Book\":[\"book1\",\"book2\",\"book4\"]}}]}}"
}

How to overcome this issue?

Thanks!

note: I added spring-mvc tag because @ResponseBody is part of spring-web

Update 1:

Tried again what @Jon Skeet has mentioned, however that gives the error,

org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: 
No serializer found for class org.json.JSONObject and no properties discovered to create 
BeanSerializer (to avoid exception, disable SerializationConfig.SerializationFeature.FAIL_ON_EMPTY_BEANS) ) 
(through reference chain: com.ht.Result["jsondata"]);
 nested exception is com.fasterxml.jackson.databind.JsonMappingException: 
 No serializer found for class org.json.JSONObject and no properties discovered 
 to create BeanSerializer (to avoid exception, disable SerializationConfig.SerializationFeature
 .FAIL_ON_EMPTY_BEANS) )
like image 692
spiderman Avatar asked Mar 19 '23 16:03

spiderman


1 Answers

From what I understand, you have

class SomePojo {
    public JSONObject json;
    @JsonInclude(Include.NON_NULL)
    @JsonProperty(value = "jsondata")
    public String getJson() {
        return json.toString();
    }
}

and

@ResponseBody
@RequestMapping(..)
public SomePojo getPojo() {
    SomePojo pojo = ...;
    return pojo;
}

The model above is basically saying that you have a JSON object which contains a name-value pair. The name is jsondata and the value is a JSON String. Since your String value contains characters that are not acceptable in a JSON String, they must be escaped in the serialized value.

But you seem to want a JSON object which contains a name-value pair where the name is jsondata and the value is another JSON object.

You probably want to have

@JsonRawValue
@JsonInclude(Include.NON_NULL)
@JsonProperty(value = "jsondata")
public String getJson() {
    return json.toString();
}

So that the String value is used as is, rather than converted to a JSON String.

like image 83
Sotirios Delimanolis Avatar answered Mar 28 '23 02:03

Sotirios Delimanolis