Creating JSON objects using Java in AJAX

By: Emiley J. Emailed: 1758 times Printed: 2452 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

To build the JSON object programmatically in Java, you can keep it simple by just constructing a StringBuffer by appending the values or you can use the JSONObject provided by json.org to construct the JSON object easily.

The simpler way to do using StringBuffer is as follows:

public String createJSON(int value){
StringBuffer rjson = new StringBuffer("\r\n{\"conversion\":{");
rjson.append("\r\n\"decimal\": \"" + Integer.toString(value)+"\",");
rjson.append("\r\n\"hexadecimal\": \"" + Integer.toString(value,16)+"\",");
rjson.append("\r\n\"Octal\": \"" + Integer.toString(value,8)+"\",");
rjson.append("\r\n\"Binary\": \"" + Integer.toString(value,2)+"\",");
rjson.append("\r\n}}");
return rjson.toString();
}

The alternate way to create the JSON object is to use the JSON library.

Remember to download and put the latest json_simple-1.0.2.jar in your CLASSPATH 

import org.json.simple.JSONObject;
JSONObject obj=new JSONObject();   
obj
.put("name","foo");   
obj
.put("num",new Integer(100));   
obj
.put("balance",new Double(1000.21));   
obj
.put("is_vip",new Boolean(true));   
obj
.put("nickname",null);   
System.out.print(obj);

 


Ajax Home | All Ajax Tutorials | Latest Ajax Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Emiley J.
Error generating R.java from manifest
ERROR 1251: Client does not support authentication protocol requested by server; consider upgrading MySQL client
IIS and Tomcat - how to configure to work together
PHP Warning: Unknown(): Unable to load dynamic library '/usr/local/php4/lib/php/extensions/no-debug ......
Adding your own Application icon for your J2ME application (jar file)
Creating Menus in Code using VB.net
Traversing all files in a directory using Ruby
Using Proxy to connect to URLs in Ruby
POST a form in Ruby
Reading URL content using Ruby (HTTP)
dRuby client/server mode sample program
Benchmark module in Ruby
encoding and decoding in Ruby using Base64 Module
Module Abbrev in Ruby
Standard Library Packages in Ruby

More Tutorials in Ajax
How to Make an HTTP Request in AJAX
Handling the Server Response in AJAX
A complete sample program in AJAX
XML vs JSON in AJAX
Creating JSON objects using Java in AJAX
The History of AJAX
To use or not to use AJAX

More Latest News
Most Viewed Articles (in Ajax )
The History of AJAX
To use or not to use AJAX
XML vs JSON in AJAX
Creating JSON objects using Java in AJAX
Handling the Server Response in AJAX
A complete sample program in AJAX
How to Make an HTTP Request in AJAX
Most Emailed Articles (in Ajax)
Creating JSON objects using Java in AJAX
How to Make an HTTP Request in AJAX
The History of AJAX
To use or not to use AJAX
XML vs JSON in AJAX
Handling the Server Response in AJAX
A complete sample program in AJAX