javascript - JSONSerializer to render a Java String Array -


i have java array containing 5 strings. want plot data using flot charts , want transfer using render(array) java file html file need javascript. have tried many things, , users suggested me pass json in java file , render make easy "digest" javascript.

one of methods i've used following one:

jsonserializer testserializer = new jsonserializer();     string test = testserializer.serialize(array);     render(test); 

i've tried store in string test[] element (as array), recognises result serialization unique variable... however, result obtain when later on assign variable ${test} in html file i've done render following one:

["hello","bye","hi"] 

with strings "hello", "bye" , "hi" placed that, absolute garbage , not useful treat javascript. furthermore, if instead of render(array) type renderjson(array), html page goes blank array shows want it, thing displayed in content.

do of have idea of how "transform" or desired ["hello", "bye", "hi"] array in javascript? thanks!

your question not clear. if understood right, want key:value relation. yes, can json , recommend use json.

the easy way use library parsing (java->json) you. recommend gson.

an easy example gson, let's have list of 4 objects type person.

public class person {     private string name;      public person(string name) {         this.name = name;     }      public string getname() {         return name;     } } 

then, add each person list

person john = new person("john"); person mark = new person("mark"); person maria = new person("maria"); person beth = new person("beth");  list<person> personlist = new arraylist<>(); personlist.add(john); personlist.add(mark); personlist.add(maria); personlist.add(beth); 

now comes parsing, let's use gson.

gson gson = new gson(); string jsonstring = gson.tojson(personlist); 

at point have string json code in it. i'm not sure if want json string or actual json object, in case want json object can following:

jsonparser parser = new jsonparser(); jsonarray jsonarray = parser.parse(jsonstring).getasjsonarray(); 

they both hold same information, 1 in string , other in json object.

to make sure, can do:

system.out.println("string: " + jsonstring); system.out.println("jsonarray.tostring(): " + jsonarray.tostring()); 

which print:

string: [{"name":"john"},{"name":"mark"},{"name":"maria"},{"name":"beth"}] jsonarray.tostring(): [{"name":"john"},{"name":"mark"},{"name":"maria"},{"name":"beth"}] 

ps; if want change key else need change on person class. lets don't want key "name" "firstname". go person class , change variable name firstname

ps2; using parsing library such small example may overkill when json object gets little bit more complex time saver.


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -