java - How to convert String to Json -


i have servlet in java , know how can following.

i have string variable value of name , want create json variable being {"name": "david"}.

how do this?

i have following code error :

   serious: servlet.service () servlet threw     exception servlet.usuarioservlet java.lang.nullpointerexception                 @ servlet.usuarioservlet.dopost (usuarioservlet.java: 166): 

at line

string mystring = new jsonobject().put("name", "hello, world!").tostring(); 

your exact problem described chandra. , may use jsonobject using suggestion. see, designers hadn't in mind properties, chaining, made success of other languages or libs.

i'd suggest use google gson one. makes both decoding , encoding easy :

the idea may define class example :

public class myclass {    public string name = "hello, world!"; }  private gson gson = new gsonbuilder().create(); printwriter writer = httpservletresponse.getwriter(); writer.write( gson.tojson(yourobject)); 

Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -