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

本文详细介绍了Java中的FastJson库如何进行对象与JSON字符串之间的转换,包括单个对象、数组以及字符串数组的处理,并展示了Maven依赖和使用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

maven依赖

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>x.x.x</version>
</dependency>

 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<Person> 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<Person> 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"}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GeekInk失控

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值