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对象
最新推荐文章于 2024-07-07 03:11:50 发布