实体类对象、字符串对象、JSONObject对象,如何互转

下面举例都用如下数据:
Student stuDemo = new Student();
List< Student> stuDemoList = new ArrayList<>();

一(1)、 普通Object类对象/List对象 转 String对象:

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

//具体方法:普通Object类对象  转  String对象
String str1 = stuDemo.toString();
String str2 = JSON.toJSONString(stuDemo);
String str3 = JSONObject.toJSONString(stuDemo);

//具体方法:List对象  转  String对象
String str4 = stuDemoList.toString();
String str5 = JSON.toJSONString(stuDemoList);
String str6 = JSONObject.toJSONString(stuDemoList);

一(2.1)、 String对象 转 普通Object对象:

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

//条件
String stuDemoStr = stuDemo.toString();
//具体方法
Student student= JSONObject.parseObject(stuDemoStr,Student.class);

一(2.2)、 String对象 转 List对象:

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

//条件
String stuDemoListStr = stuDemoList.toString();
//具体方法
List<Student> studentList= JSONObject.parseArray(stuDemoListStr,Student.class);

二(1)、 String对象 转 JSON对象:

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

//条件
String stuDemoStr = JSON.toJSONString(stuDemo);
//具体方法
JSONObject jsonObject= JSON.parseObject(stuDemoStr);

二(2)、 JSON对象 转 String对象:

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

//Json对象条件
String stuDemoStr = JSON.toJSONString(stuDemo);
JSONObject jsonObject= JSON.parseObject(stuDemoStr);
//具体方法
String str = jsonObject.toString();
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值