JSONObject example in Java
In the previous section of JSON tutorials youhave 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:
- commons-lang.jar
- commons-beanutils.jar
- commons-collections.jar
- commons-logging.jar
- ezmorph.jar
- 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,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
importnet.sf.json.JSONObject; |
To run this example you have to follow these few stepsas follows:
- Download JSON-lib jar and other supporting Jars
- Add these jars to your classpath
- create and save FirstJSONJava.java
- Compile it and execute,You will get following output:
Output:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。