json字符串转换JSONObject的两种方式(无对应实体类)

1. 使用google依赖


    com.vaadin.external.google
    android-json
    0.0.20131108.vaadin1
    test
    import org.json.JSONException;
    import org.json.JSONObject;

    @Test
    public void testJsonToObject() throws JSONException {
        //把json字符串转为json对象
        String jsonString = "{\"notBefore\":\"2019.11.09 15:09:00\",\"notAfter\":\"2022.07.24 15:09:00\"}";
        JSONObject jsonObj = new JSONObject(jsonString);
        System.out.println(jsonObj.get("notBefore"));
    }

 2. 使用alibaba依赖


    com.alibaba
    fastjson
    1.2.28

    import com.alibaba.fastjson.JSONObject;

    @Test
    public void testJsonToObject2(){
        String ss = "{\"notBefore\":\"2019.11.09 15:09:00\",\"notAfter\":\"2022.07.24 15:09:00\"}";
        JSONObject json = JSONObject.parseObject(ss);
        System.out.println(json.getString("notBefore"));
    }

 

你可能感兴趣的:(学习笔记)