java中使用json
java中使用json需要引入第三方json项目
json官网: http://json.org
json官网页面往下拉,可以找到Java板块,该板块列出了官方推荐的第三方json项目
之前开发用的Json-lib,但今天发现org.json更简便(Json-lib依赖的jar较多)
本文将介绍org.json的用法
1. 下载org.json
org.json的官网: http://www.json.org/java/index.html。点击官网页面的”Free
source code is available”,即可转到org.json在github的源码页,点击页面右侧的”Download ZIP”,下载”JSON-java-master.zip”文件,解压该文件。在Eclipse中新建package,命名”org.json”,然后将”JSON-java-master”里的文件拷到”org.json”,即可在项目使用json
也可以将org.json打成jar包,方便以后使用。右键org.json文件—Export—Java—JAR file,点”Next”,选中org.json、org.json.zip文件夹,然后输入jar文件的输出路径
2. json的用法
2.1 初始化json对象
json常用的对象是JSONObject、JSONArray
- package TestJson;
- import org.json.JSONArray;
- import org.json.JSONObject;
- public class TestJson01 {
- public static void main(String[] args) {
- //初始化JSONObject 方法一
- JSONObject jsonObject1 = new JSONObject();
- jsonObject1.put(“Name”, “Tom”);
- jsonObject1.put(“age”, “11”);
- //初始化JSONObject 方法二
- JSONObject jsonObject2 = new JSONObject(“{\’Name\’:\’Tom\’,\’age\’:\’11\’}”);
- //初始化JSONArray 方法一
- JSONArray jsonArray1 = new JSONArray();
- jsonArray1.put(“abc”);
- jsonArray1.put(“xyz”);
- //初始化JSONArray 方法二
- JSONArray jsonArray2 = new JSONArray(“[\’abc\’,\’xyz\’]”);
- System.out.println(“jsonObject1:” + “\r” + jsonObject1.toString());
- System.out.println(“jsonObject2:” + “\r” + jsonObject2.toString());
- System.out.println(“jsonArray1:” + “\r” + jsonArray1.toString());
- System.out.println(“jsonArray2:” + “\r” + jsonArray2.toString());
- }
- }
Output:
- jsonObject1:
- {“Name”:”Tom”,”age”:”11″}
- jsonObject2:
- {“Name”:”Tom”,”age”:”11″}
- jsonArray1:
- [“abc”,”xyz”]
- jsonArray2:
- [“abc”,”xyz”]
2.2 JSONObject、JSONArray相互嵌套
- package TestJson;
- import org.json.JSONArray;
- import org.json.JSONObject;
- public class TestJson02 {
- public static void main(String[] args) {
- JSONObject jsonObject = new JSONObject(“{\’Name\’:\’Tom\’,\’age\’:\’11\’}”);
- JSONArray jsonArray = new JSONArray(“[\’abc\’,\’xyz\’]”);
- //JSONObject内嵌JSONObject
- JSONObject jsonObject1 = new JSONObject();
- jsonObject1.put(“object1”, jsonObject);
- jsonObject1.put(“object2”, jsonObject);
- //JSONObject内嵌JSONArray
- JSONObject jsonObject2 = new JSONObject();
- jsonObject2.put(“JSONArray1”, jsonArray);
- jsonObject2.put(“JSONArray2”, jsonArray);
- //JSONArray内嵌JSONArray
- JSONArray jsonArray1 = new JSONArray();
- jsonArray1.put(jsonArray);
- jsonArray1.put(jsonArray);
- //JSONArray内嵌JSONObject
- JSONArray jsonArray2 = new JSONArray();
- jsonArray2.put(jsonObject);
- jsonArray2.put(jsonObject);
- System.out.println(“JSONObject内嵌JSONObject:” + “\r” + jsonObject1.toString());
- System.out.println(“JSONObject内嵌JSONArray:” + “\r” + jsonObject2.toString());
- System.out.println(“JSONArray内嵌JSONArray:” + “\r” + jsonArray1.toString());
- System.out.println(“JSONArray内嵌JSONObject:” + “\r” + jsonArray2.toString());
- }
- }
Output:
- JSONObject内嵌JSONObject:
- {“object1”:{“Name”:“Tom”,“age”:“11”},“object2”:{“Name”:“Tom”,“age”:“11”}}
- JSONObject内嵌JSONArray:
- {“JSONArray2”:[“abc”,“xyz”],“JSONArray1”:[“abc”,“xyz”]}
- JSONArray内嵌JSONArray:
- [[“abc”,“xyz”],[“abc”,“xyz”]]
- JSONArray内嵌JSONObject:
- [{“Name”:“Tom”,“age”:“11”},{“Name”:“Tom”,“age”:“11”}]
开发中根据需求进行内嵌
2.3 解析json数据
- package TestJson;
- import org.json.JSONObject;
- public class TestJson03 {
- public static void main(String[] args) {
- //解析字符串
- JSONObject jsonObject1 = new JSONObject();
- jsonObject1.put(“Name”, “Tom”);
- jsonObject1.put(“age”, “11”);
- System.out.println(“解析字符串:” + “\r” + jsonObject1.getString(“Name”));
- //解析对象
- JSONObject jsonObject = new JSONObject(“{\’Name\’:\’Tom\’,\’age\’:\’11\’}”);
- JSONObject jsonObject2 = new JSONObject();
- jsonObject2.put(“jsonObject”, jsonObject);
- System.out.println(“解析对象:”);
- System.out.println(jsonObject2.getJSONObject(“jsonObject”).toString());
- }
- }
Output:
- 解析字符串:
- Tom
- 解析对象:
- {“Name”:”Tom”,”age”:”11″}
版权声明:本文为博主原创文章,未经博主允许不得转载。