JSONObject——JSONArray——Map——String——List之间转换

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

import java.util.*;

/**
 * Created by admin on 2018/7/17.
 */
public class test {

    public static void main(String[] args) {


//        1.String转JSONObject
        String jsonString = "{\"Chinese\":\"88\",\"Math\":\"78\",\"English\":\"99\"}";
        JSONObject myJson = JSONObject.fromObject(jsonString);
        System.out.println("Chinese:" + myJson.get("Chinese"));
        System.out.println("Math:" + myJson.get("Math"));
        System.out.println("English:" + myJson.get("English"));

        System.out.println("------------------String转JSONObject------------------------");


//        2.String转JSONArray
        String jsonMessage = "[{'num':'成绩', '外语':88, '历史':65, '地理':99, 'object':{'aaa':'1111','bbb':'2222','cccc':'3333'}}," +
                "{'num':'兴趣', '外语':28, '历史':45, '地理':19, 'object':{'aaa':'11a11','bbb':'2222','cccc':'3333'}}," +
                "{'num':'爱好', '外语':48, '历史':62, '地理':39, 'object':{'aaa':'11c11','bbb':'2222','cccc':'3333'}}]";
        JSONArray myJsonArray = JSONArray.fromObject(jsonMessage);
        System.out.println(myJsonArray);
        System.out.println("------------------String转JSONArray------------------------");


//        String转Map
        jsonMessage = "{\"Chinese\":\"88\",\"Math\":\"78\",\"English\":\"99\"}";
        Map m = (Map) JSONObject.fromObject(jsonMessage);
        System.out.println("Chinese:" + m.get("Chinese"));
        System.out.println("Math:" + m.get("Math"));
        System.out.println("English:" + m.get("English"));
        System.out.println("------------------String转Map------------------------");


//        List转JSONArray
        List list = new ArrayList();
        list.add("{'num':'成绩', '外语':88, '历史':65, '地理':99, 'object':{'aaa':'1111','bbb':'2222','cccc':'3333'}}");
        list.add("{'num':'兴趣', '外语':28, '历史':45, '地理':19, 'object':{'aaa':'11a11','bbb':'2222','cccc':'3333'}}");
        list.add("{'num':'爱好', '外语':48, '历史':62, '地理':39, 'object':{'aaa':'11c11','bbb':'2222','cccc':'3333'}}");
        JSONArray jsonArray2 = JSONArray.fromObject(list);
        System.out.println(jsonArray2);
        System.out.println("------------------jsonArray2------------------------");

//        JSONArray转List
        List> mapListJson = (List) jsonArray2;
        System.out.println(mapListJson);
        System.out.println("------------------mapListJson------------------------");


//        String转数组

        String string = "a,b,c";
        String[] stringArr = string.split(",");  //注意分隔符是需要转译滴... 如果是"abc"这种字符串,就直接
        System.out.println("stringArr:" + Arrays.asList(stringArr));
        string = "abc";
        char[] stringArr1 = string.toCharArray(); //注意返回值是char数组 如果要返回byte数组就直接使用getBytes方法就ok了 ~~
        System.out.println("stringArr1:" + Arrays.toString(stringArr1));    //基本类型可以用Arrays.toString
        string = "abc";
        byte[] stringArr2 = string.getBytes();
        System.out.println("stringArr2:" + Arrays.toString(stringArr2)); //基本类型可以用Arrays.toString
//        数组转String
        char[] data = {'a', 'b', 'c'};
        String s = new String(data);
        System.out.println("s=" + s);

    }


}

输出结果:

JSONObject——JSONArray——Map——String——List之间转换_第1张图片

你可能感兴趣的:(java基础)