JSONObject example in Java

n the previous section of JSON tutorials you have seen how JSON can be used with JavaScript to create objects and arrays, you have also studied how to parse and create messages with JSON in JavaScript.

JSONObject example in Java

In the previous section of JSON tutorials you have seen how JSON can be used with JavaScript to create objects and arrays, youhave also studied how to parse and create messages with JSON in JavaScript. Nowin this part you will study how to use JSON in Java.

To have functionality of JSON in java you must haveJSON-lib. JSON-lib also requires following "JAR" files:

  1. commons-lang.jar 
  2. commons-beanutils.jar
  3. commons-collections.jar
  4. commons-logging.jar
  5. ezmorph.jar
  6. json-lib-2.2.2-jdk15.jar

JSON-lib is a java library for that transforms beans,collections, maps, java arrays and XML to JSON and then forretransforming them back to beans, collections, maps and others.

In this example we are going to use JSONObjectclass for creating an object of JSONObject and then we will print theseobject value. For using JSONObject class we have to import followingpackage "net.sf.json". To add elements in this object we haveused put() method. Here is the full example code of FirstJSONJava.java isas follows:

FirstJSONJava.java

import net.sf.json.JSONObject;

public class FirstJSONJava
{
  public static void main(String args[]){
  JSONObject object=new JSONObject();
 object.put("name","Amit Kumar");
 object.put("Max.Marks",new Integer(100));
 object.put("Min.Marks",new Double(40));
 object.put("Scored",new Double(66.67));
 object.put("nickname","Amit");
 System.out.println(object);
  }
}  

 
To run this example you have to follow these few stepsas follows:

Output:

 
Download Source Code

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值