Skip to content Skip to sidebar Skip to footer

How To Convert Java Object To Json String?

Hello i'm trying to convert my Java Object to a Json string to use in my view with javascript functions. I tried to use Google's gson lib: String myjson = ''; Gson gson = new Gson(

Solution 1:

Note that "&quot" is "Proper" JSON, so your javascript can be escaped properly, anyways if you dont want to use entities in you JSON perhaps you want to disable html formatting/escaping.

Try using

Gsongson=newGsonBuilder().disableHtmlEscaping().create();

Check the GSONBuilder Object Documentation http://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/GsonBuilder.html

Solution 2:

I think he's protecting you against html special chars.

Try something like that:

Gsongson=newGson();
Stringtarget="my text";
Stringjson= gson.toJson(target);

This code is extract from http://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/Gson.html (the api).

Post a Comment for "How To Convert Java Object To Json String?"