Reputation: 2607
My situation is crazy. I'm creating a JSON object, which is perfectly fine from server side end. But, this is not getting acceptable due to extra " which is created via server side. My server side code is
String id = "123";
String hql = "FROM Person E WHERE E.userId = "+id;
Query query = session.createQuery(hql);
List<?> results = query.list();
JSONArray arr = new JSONArray();
JSONObject obj = new JSONObject();
for(int i = 0; i < results.size(); i++) {
Personalisation p = (Personalisation) results.get(i);
obj.put("courseId", p.getCourseId());
obj.put("CourseValue", p.getCourseValue());
}
System.out.println(obj);
It is printing
{"CourseValue":"{\"color\": \"green\",\"value\": \"#f00\"}","courseId":"C5"}
This is fine from server side end. You could see extra " before {\"color tag, when I try to parse the same thing on client side, it doesnt accept due to illegal character. What should I need to do?
Here is the fiddle too http://jsfiddle.net/hLkUz/43/
Upvotes: 0
Views: 78
Reputation: 2607
Able to construct by using
new JsonParser().parse(p.getCourseValue()).getAsJsonObject()
Upvotes: 0
Reputation: 31
as far as I know you cannot parse a JSON object as a string so use
System.out.println(obj.toString());
instead of
System.out.println(obj);
Upvotes: 0
Reputation: 32980
p.getCourseValue()
seems to return a String containing JSON:
{"color": "green","value": "#f00"}
Now when you put this into JSONObject
and serialize the object it will escape the JSON string again.
Instead of putting the course value as JSON tring into the JSONObject
you need to build up the course value object yourself.
Upvotes: 4