当前位置: 首页 > 工具软件 > JSON for .NET > 使用案例 >

JSON用法—net.sf.json.JSONObject

梁丘宏硕
2023-12-01
      JSONObjetc和JSONArray存在多种用法,下面介绍JSONObject和JSONArray的创建、合并以及解析。
import java.util.ArrayList;
import java.util.HashMap;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class JsonCombine {

	public static void main(String[] args) {
		JSONObject jsonOne = new JSONObject();
		jsonOne.put("name", "kewen");
		jsonOne.put("age", "24");
		JSONObject jsonTwo = new JSONObject();
		jsonTwo.put("hobbit", "Doto");
		jsonTwo.put("hobbit2", "wow");
		JSONObject jsonThree = new JSONObject();
		jsonThree.putAll(jsonOne);
		jsonThree.putAll(jsonTwo);
		System.out.println(jsonThree.toString());

		// JsonObject和JsonArray区别就是JsonObject是对象形式,JsonArray是数组形式
		// 创建JsonObject第一种方法
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("UserName", "ZHULI");
		jsonObject.put("age", "30");
		jsonObject.put("workIn", "ALI");
		System.out.println("jsonObject1:" + jsonObject);

		// 创建JsonObject第二种方法
		HashMap<String, String> hashMap = new HashMap<String, String>();
		hashMap.put("UserName", "ZHULI");
		hashMap.put("age", "30");
		hashMap.put("workIn", "ALI");
		System.out.println("jsonObject2:" + JSONObject.fromObject(hashMap));

		// 创建一个JsonArray方法1
		JSONArray jsonArray = new JSONArray();
		jsonArray.add(0, "ZHULI");
		jsonArray.add(1, "30");
		jsonArray.add(2, "ALI");
		System.out.println("jsonArray1:" + jsonArray);

		// 创建JsonArray方法2
		ArrayList<String> arrayList = new ArrayList<String>();
		arrayList.add("ZHULI");
		arrayList.add("30");
		arrayList.add("ALI");
		System.out.println("jsonArray2:" + JSONArray.fromObject(arrayList));
		// 如果JSONArray解析一个HashMap
		System.out.println("jsonArray FROM HASHMAP:" + JSONArray.fromObject(hashMap));
		// 组装一个复杂的JSONArray
		JSONObject jsonObject2 = new JSONObject();
		jsonObject2.put("UserName", "ZHULI");
		jsonObject2.put("age", "30");
		jsonObject2.put("workIn", "ALI");
		jsonObject2.element("Array", arrayList);
		jsonObject2.element("Map", hashMap);
		System.out.println("jsonObject2:" + jsonObject2);

		// 将Json字符串转为java对象
		String jsonString = "{\"UserName\":\"ZHULI\",\"age\":\"30\",\"workIn\":\"ALI\",\"Array\":[\"ZHULI\",\"30\",\"ALI\"]}";
		JSONObject obj = JSONObject.fromObject(jsonString);
		if (obj.has("UserName")) {
			System.out.println("UserName:" + obj.getString("UserName"));
		}
		if (obj.has("Array")) {
			JSONArray transitListArray = obj.getJSONArray("Array");
			for (int i = 0; i < transitListArray.size(); i++) {
				System.out.println("Array:" + transitListArray.getString(i) + " ");

			}
		}

	}

}

       运行结果:

{"name":"kewen","age":"24","hobbit":"Doto","hobbit2":"wow"}
jsonObject1:{"UserName":"ZHULI","age":"30","workIn":"ALI"}
jsonObject2:{"UserName":"ZHULI","workIn":"ALI","age":"30"}
jsonArray1:["ZHULI","30","ALI"]
jsonArray2:["ZHULI","30","ALI"]
jsonArray FROM HASHMAP:[{"UserName":"ZHULI","workIn":"ALI","age":"30"}]
jsonObject2:{"UserName":"ZHULI","age":"30","workIn":"ALI","Array":["ZHULI","30","ALI"],"Map":{"UserName":"ZHULI","workIn":"ALI","age":"30"}}
UserName:ZHULI
Array:ZHULI 
Array:30 
Array:ALI 



 类似资料: