json与string转换:com.alibaba.fastjson.JSONObject

Java 序列化是一种将对象转换为字节流的过程,以便可以将对象保存到磁盘上,将其传输到网络上,或者将其存储在内存中,以后再进行反序列化,将字节流重新转换为对象。

maven依赖


    com.alibaba
    fastjson
    x.x.x

 FastJson - 将JSON字符串转换为java对象

 public static void main(String[] args) {
    String json = "{\"createTime\":1621341922450,\"id\":1,\"name\":\"DT\"}";
    // 方式1: 将json字符串转为Java对象
    Person person = JSON.parseObject(json,Person.class);
    System.out.println("java对象->>>"+person);
    System.out.println("=========================================================");
    // 方式2: 将json字符串转为Java对象
    Person newPerson2 = JSONObject.parseObject(json, Person.class);
    System.out.println("java对象->>>"+newPerson2);
}

 FastJson - 将java对象转换为JSON字符串

public static void main(String[] args) {
     Person person = new Person(1,"DT",new Date());
     // 方式1:将对象转为json字符串
     String json = JSON.toJSONString(person);
     System.out.println("json字符串->>>"+json);
     System.out.println("=========================================================");
     // 方式2:将对象转为json字符串
     String json2 = JSONObject.toJSONString(person);
     System.out.println("json字符串->>>"+json2);
 }

FastJSON - 将JSON字符串数组转换为JSON数组

public static void main(String[] args) {
    List personList = new ArrayList<>();
    personList.add(new Person(1,"DT",new Date()));
    personList.add(new Person(2,"DT1",new Date()));
    personList.add(new Person(3,"DT2",new Date()));
    String json = JSONObject.toJSONString(personList);
    JSONArray jsArr = JSONObject.parseArray(json);
    System.out.println(jsArr);
}

循环遍历JSON数组

 public static void main(String[] args) {
   List personList = new ArrayList<>();
    personList.add(new Person(1,"DT",new Date()));
    personList.add(new Person(2,"DT1",new Date()));
    personList.add(new Person(3,"DT2",new Date()));
    String json = JSONObject.toJSONString(personList);
    JSONArray jsArr = JSONObject.parseArray(json);
    // 遍历方式1
    jsArr.stream().forEach(json1 ->{
        System.out.println(json1.toString());
    });
    // 遍历方式2
    for (Object o : jsArr) {
        JSONObject obj = (JSONObject) o;
        System.out.println("取到id->>>" + obj.get("id"));
        // 判断是否存在key
        System.out.println("key是否存在->>>"+obj.containsKey("name1"));
        // 判断是否存在值
        System.out.println("value是否存在->>>"+obj.containsValue(obj.get("id")));
    }
}

String 转换为 json 对象

public class Str2Json {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
	String str = "{\"cases\"[{\"classname\":\"HttpGet\",\"url\":\"https://www.baidu.com\"},{\"classname\":\"HttpPost\",\"url\":\"https:www.qq.com\"}]}";
	JSONObject json = JSON.parseObject(str);
	System.out.println(json);
    }
}

输出结果:
{"cases":[{"classname":"HttpGet","url":"https://www.baidu.com"},{"classname":"HttpPost","url":"https:www.qq.com"}]}

可以通过json提取相关的内容

public class Str2Json{
    public static void main(String[] args){
        String str = "{\"cases\"[{\"classname\":\"HttpGet\",\"url\":\"https://www.baidu.com\"},{\"classname\":\"HttpPost\",\"url\":\"https:www.qq.com\"}]}";
    	JSONObject json = JSON.parseObject(str);
        System.out.println(json.getString("cases"));
    }
}

输出结果:
[{"classname":"HttpGet","url":"https://www.baidu.com"},{"classname":"HttpPost","url":"https:www.qq.com"}]

String转JSONArray

public class Str2Json {
    public static void main(String[] args) {
    	String str = "{\"cases\"[{\"classname\":\"HttpGet\",\"url\":\"https://www.baidu.com\"},{\"classname\":\"HttpPost\",\"url\":\"https:www.qq.com\"}]}";
    	JSONObject json = JSON.parseObject(str);
        String jsonstr = json.getString("cases");
    	System.out.println(JSONArray.parseArray(jsonstr));
    }
}


输出结果:
[{"classname":"HttpGet","url":"https://www.baidu.com"},{"classname":"HttpPost","url":"https:www.qq.com"}]

可以看出jsonstr是个String类型,获取到里面完整的元素有点困难,转成JSONArray类型后,与使用list元素类似

public class Str2Json {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
	String str = "{\"cases\"[{\"classname\":\"HttpGet\",\"url\":\"https://www.baidu.com\"},{\"classname\":\"HttpPost\",\"url\":\"https:www.qq.com\"}]}";
	JSONObject json = JSON.parseObject(str);
    String jsonstr = json.getString("cases");
	System.out.println(JSONArray.parseArray(jsonstr).get(0));
    }
}


{"classname":"HttpGet","url":"https://www.baidu.com"}

你可能感兴趣的:(json)