javascript - Gson generate json with ",js throw error -


i use gson create string like: new gson.tojson(englishresult); englishresult list> type. result like:

[{"name":"en3","value":234},{"name":"en4","value":135},{"name":"en1","value":335},{"name":"en2","value":310},{"name":"en5","value":1548}] 

and invoke js method throw error

uncaught syntaxerror: unexpected token &

i search goole,it tells me &quot json,because can contain escape,but why js throws error? how solve it? plz tell me suggestion,thanks much

my server code :

 public static void pietestwithdata(){          map<string, integer> result = getchineseresultmap();         list<map<string, object>> englishresult = getformatteredpiedata();         renderargs.put("resultdata", getjson(englishresult));         render();     }   private static string getjson(list<map<string, object>> englishresult) {         gson gson = new gsonbuilder()                 .disablehtmlescaping()                 .create();         return gson.tojson(englishresult);     } 

(posted here because stackoverflow comments limited 264 characters)

@wangyiran

i unable reproduce. here test:

public class toto {     private string name;     public string getname() {         return name;     }     public void setname(string name) {         this.name = name;     } } 

then

    @test     public void testme() {         list<map<string, object>> englishresult = new arraylist<>();         toto toto = new toto();         toto.setname("toto it's me");         map<string, object> m = new hashmap<string, object>();         m.put("mapkey", toto);         englishresult.add(m);          gson gson = new gson();         string jsonenglishresult = gson.tojson(englishresult);         system.out.println(jsonenglishresult);     } 

test output:

[{"mapkey":{"name":"toto it\u0027s me"}}] 

i'm using gson version 2.3.1.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -