Monday, March 25, 2013

Re: JSONParser.parseStrict() with \"

For my case, I want to keep \". So I'd need to do 

replaceAll("\"","\\\"")

on all my java object toString() when I want to convert this back to JSON format. Is there a better GWT library to do this conversion? 

This is now I convert back to JSON string: 

public String toString(){

    return "{" + 

    "\"name\":" + ((name == null)? "null" : ("\"" + name + "\"")) +

    ", \"index\":" + index +

    ", \"text\":" + ((text == null)? "null" : ("\"" + text + "\"")) +

    "}";

    }


On Monday, March 25, 2013 1:23:43 PM UTC-7, rkulisas wrote:
Hi Thomas,

I don't have control over the string value that gets passed in to me. If I don't get \" in return, the JSON will be invalid. I'm having similar problem to this thread. http://stackoverflow.com/questions/13420115/padding-quotes-in-jsonobject

Thank you

On Monday, March 25, 2013 3:57:19 AM UTC-7, Thomas Broyer wrote:


On Monday, March 25, 2013 11:23:51 AM UTC+1, rkulisas wrote:

//josn is {"name":"item_name","index":0,"text":"Kindle Fire HD 8.9\"..."}

JSONValue value = JSONParser.parseStrict(json);

JSONObject obj;

JSONString name, text;

JSONNumber nxd;

if ((obj = value.isObject()) == null ) return null;

if ((value = obj.get("name")) != null) 

if ((name = value.isString()) != null)

   field.setName(name.stringValue());

if ((value = obj.get("text")) != null) 

if ((text = value.isString()) != null){

System.out.println("VALUE:" + value.toString());  //VALUE:"Kindle Fire HD 8.9\" ..."

System.out.println("TEXT:" + text.stringValue()); //TEXT:Kindle Fire HD 8.9" ...

field.setText(text.stringValue());

}

Am I supposed to use JSONValue.toString() to get my string value instead? Why JSONString.stringValue() doesn't parse properly?

The question is more: why do you want the \ to be there?
It's in your JSON because JSON uses " to markup string literals and thus has to escape " that appears within the string value, just like you do in Java or JavaScript or so many other languages (C, etc.)

stringValue() *does* return the correct value, it's just that you're expecting something else.

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment