JSON

import net.sf.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class Hello {

public static void main(String[] args) {
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("user", "vinter");
    jsonObject.put("age", "16");

    System.out.println(jsonObject);

    jsonObject.remove("age");
    System.out.println(jsonObject);

    jsonObject.put("user", "hello");
    System.out.println(jsonObject);

    String user = (String) jsonObject.get("user");
    System.out.println(user);

    ArrayList arrayList = new ArrayList();
    arrayList.add("you");
    jsonObject.put("arr", arrayList);
    jsonObject.element("arr1", arrayList);
    
    System.out.println("arr:"+jsonObject);


    Map map = new HashMap();
    map.put("one", "hello");
    map.put("two", "world");

    System.out.println("----------------------");

    JSONObject jsonObject1 = JSONObject.fromObject(map);
    System.out.println(jsonObject1);

}

}

import net.sf.json.JSONArray;
import java.util.ArrayList;

public class Hello {

public static void main(String[] args) {
    JSONArray jsonArray = new JSONArray();
    jsonArray.add(0, "ZHULI");
    jsonArray.add(1, "30");
    jsonArray.add(2, "ALI");
    System.out.println("jsonArray1:" + jsonArray);

    ArrayList arrayList = new ArrayList();
    arrayList.add("ZHULI");
    arrayList.add("30");
    arrayList.add("ALI");
    arrayList.add(1, "27");
    System.out.println("jsonArray2:" + arrayList);
    System.out.println("jsonArray3:" + JSONArray.fromObject(arrayList));

}

}

你可能感兴趣的:(JSON)