Angelof7 / google-gson

Automatically exported from code.google.com/p/google-gson
0 stars 0 forks source link

JsonObject.addProperty method is tricky when dealing with json string #549

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. I had a Map datastructure from my servlet code. and I was trying to send 
this to my client code by 
new JsonObject().addProperty("jsondata", new Gson().toJson(<my-map>, type))
2. I am sending this obj to client by setting appropriate response content type
3. I am able to recieve this jsondata at the client side

What is the expected output? What do you see instead?
As I had send this Map from my server code I was expecting the js object at the 
client side but instead when I was iterating over this object I realized that 
it is being treated as string and not as js object.

What version of the product are you using? On what operating system?
I am using following configuration
Windows 7 32 bit
Tomcat 7 server
java 1.6
Jquery 1.9 @ client side and underscore.js
google-collections-0.8.jar
gson-2.2.4.jar

Please provide any additional information below.

When I am doing 
String jsondata = new Gson().toJson(<my-Map>, type);
and sending this to client I am able to iterate over this as this is being 
treated as a js object.

Please let me know if you need any additional information.

Original issue reported on code.google.com by shantano...@gmail.com on 13 Dec 2013 at 6:40

GoogleCodeExporter commented 9 years ago
I wouldn't expect anything else from `addProperty(String, String)`. It's just a 
String. Try this instead:

new JsonObject().add("jsondata", new Gson().toJsonTree(<my-map>, type))

Original comment by Maaarti...@gmail.com on 15 Dec 2013 at 11:03