Net.sf.json.JSOnObject使用
5840 ワード
転載先:https://blog.csdn.net/u013514928/article/details/79114814/
JSOnObjetcとJSOnArrayには様々な使い方がありますが、JSOnObjectとJSOnArrayの作成、マージ、解析について説明します.
[java] view plain
copy
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を作成する2つ目の方法
HashMap hashMap = new HashMap();
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 arrayList = new ArrayList();
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
System.out.println("Array:" + transitListArray.getString(i) + " ");
}
}
}
}
実行結果:
[java] view plain
copy
{"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
optXX(key,defaultValue)はgetXX(key)と区別され、getXX(key)はkeyが存在しないと異常を放出し、
optXX()はdefafltValueを返します
上のコード:
実行結果:
JSOnObjetcとJSOnArrayには様々な使い方がありますが、JSOnObjectとJSOnArrayの作成、マージ、解析について説明します.
[java] view plain
copy
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を作成する2つ目の方法
HashMap hashMap = new HashMap();
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 arrayList = new ArrayList();
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
System.out.println("Array:" + transitListArray.getString(i) + " ");
}
}
}
}
実行結果:
[java] view plain
copy
{"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
optXX(key,defaultValue)はgetXX(key)と区別され、getXX(key)はkeyが存在しないと異常を放出し、
optXX()はdefafltValueを返します
上のコード:
String jsonStr="{\"name\":\"xy\",\"age\":24}";
JSONObject json= JSONObject.fromObject(jsonStr);
System.out.println("json.opt(name)---"+json.opt("name"));
System.out.println("json.optInt(age, 22)---"+json.optInt("age", 22));
System.out.println("json.optString(age)---"+json.optString("age"));
System.out.println("json.opt(aaa)---"+json.opt("aaa"));
実行結果:
json.opt(name)---xy
json.optInt(age, 22)---24
json.optString(age)---24
json.opt(aaa)---null