import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
public class CreateJsonObjTest {
private static void toJson() {
JSONObject jsonObject = new JSONObject();
try {
//形式1 普通
jsonObject.put("a", "a_value");
jsonObject.put("b", "b_value");
//形式2 集合
JSONArray json_c = new JSONArray();
json_c.add("c1_value");
json_c.add("c2_value");
jsonObject.put("c", json_c);
//形式3 對象
JSONObject json_d = new JSONObject();
json_d.put("d1", "d1_value");
json_d.put("d2", "d2_value");
jsonObject.put("d", json_d);
//形式4 集合中嵌套對象
JSONArray json_e = new JSONArray();
JSONObject childer_1 = new JSONObject();
childer_1.put("e1", "e1_value");
childer_1.put("e2", "e2_value");
json_e.add(childer_1);
JSONObject childer_2 = new JSONObject();
childer_2.put("e3", "e3_value");
childer_2.put("e4", "e4_value");
json_e.add(childer_2);
jsonObject.put("e", json_e);
} catch (JSONException e) {
e.printStackTrace();
}
System.out.println(jsonObject.toString());
}
public static void main(String[] args) {
long befor = System.currentTimeMillis();
toJson();
long now = System.currentTimeMillis();
System.out.println(now-befor);
}
}
創建json對象
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.