String和JSON相互转换

本文介绍了如何在Java中使用Fastjson库进行对象到JSON字符串,JSON字符串到JSONObject,以及JSONArray与字符串之间的转换。示例包括将对象转化为JSONStr,将对象转化为JSONObject,将String转换为JSONArray,将JSONArray转换回字符串,以及Fastjson在字符串和对象间的转换操作。

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

1.JSON格式的字符串转对象

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
//String转对象
String message = "{\"name\":\"John\", \"age\":30, \"city\":\"New York\"}";
Person person= JSONObject.parseObject(message, new TypeReference<Person>(){});

//其中person的属性如下
@Data
public class Person {
	private String name;
	private int age;
	private String city;
}

2.对象转Json格式的字符串

import com.alibaba.fastjson.JSON;

String jsonString = JSON.toJSONString(person);
System.out.println(jsonString);
//结果
{"age":12,"city":"北京","name":"张三"}

3.对象转JSONObject

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

JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(person));
System.out.println(jsonObject);
//结果
{"city":"北京","name":"张三","age":12}

4.将 json格式的字符串 转换 为JSONArray:

        方法一:

String str = "[{\"name\":\"Alice\",\"age\":20},{\"name\":\"Bob\",\"age\":30}]";
JSONArray jsonArray = JSON.parseArray(str);

         方法二:

String jsonString = "[\"value1\",\"value2\"]";
JSONArray jsonArray = new JSONArray(jsonString);

5.将JSONArray转换为 json格式的字符串:

         方法一:

JSONArray jsonArray = new JSONArray();
JSONObject obj1 = new JSONObject();
obj1.put("name", "Alice");
obj1.put("age", 20);
JSONObject obj2 = new JSONObject();
obj2.put("name", "Bob");
obj2.put("age", 30);
jsonArray.add(obj1);
jsonArray.add(obj2);
String str = jsonArray.toJSONString();

        方法二:

JSONArray jsonArray = new JSONArray();
jsonArray.put("value1");
jsonArray.put("value2");
String jsonString = jsonArray.toString();

6. 对象集合转JSONArray

//对象集合转JSONArray 
List<Station> stations = List.of(new Station("北京站", "BJS"),new Station("上海站", "SHH"));
JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(stations));

7.JSONObject和字符串的转换

import com.alibaba.fastjson.JSONObject;
#存入属性
JSONObject jsonObject = new JSONObject();
jsonObject.put("name", "John");
jsonObject.put("age", 30);
String myStr = jsonObject.toJSONString();
#取出属性
JSONObject jsonObject1 = JSON.parseObject(myStr);
String name = jsonObject1.getString("name");

8.list和map转json格式的字符串

import com.alibaba.fastjson.JSON;
//list和map转json格式的字符串
 List<Map<String,Object>> referTeamTradeTopList = new ArrayList<>();
 HashMap<String, Object> map = new HashMap<>();
 map.put("realname","zhangs");
 map.put("productName","haike");
 map.put("totalAmount",12);
 referTeamTradeTopList.add(map);
//打印
 String s1 = JSON.toJSONString(map);
 //s1: {"totalAmount":12,"productName":"haike","realname":"zhangs"}
 String s2 = JSON.toJSONString(referTeamTradeTopList);
 //s2: [{"totalAmount":12,"productName":"haike","realname":"zhangs"}]

9.根据需求的json格式拼接数据

#需求的json格式
{
    "openStatus": true,
    "timeSchedule": [
        [
            "1 00:00:00-24:00:00"
        ],
        [
            "1 00:00:00-24:00:00"
        ]
    ],
    "channelId": "001"
}

实现方法

    public static void main(String[] args) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("channelId","001");
        boolean openStatus = "on".equals("on");
        jsonObject.put("openStatus",openStatus);
        String timeScheduleEntry = "1 00:00:00-24:00:00";

        List<String> timeScheduleList = new ArrayList<>();
        timeScheduleList.add(timeScheduleEntry);
        List<List<String>> lists = new ArrayList<>();

        for (int i = 0; i < 2; i++) {
            lists.add(new ArrayList<>(timeScheduleList));
        }
        jsonObject.put("timeSchedule", lists);
        System.out.println(JSON.toJSONString(jsonObject));
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值