0

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/

0

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I tried it using new JSONObject(p.getCourseValue()). Still its not accepting. Any info?
0

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);

6 Comments

and use obj.put("CourseValue", new JSONObject(p.getCourseValue())); while inserting
Its not accepting new JSONObject(p.getCourseValue()). It says remove argument
and I'm not worried about SOP. I'm worried because its not being accepted by client
give a try to (JSONObject)new JSONParser().parse(p.getCourseValue());
Yeah I tried by new JsonParser().parse(p.getCourseValue()).getAsJsonObject()
|
0

Able to construct by using

new JsonParser().parse(p.getCourseValue()).getAsJsonObject()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.