JSON数据的拼接与解析

本文介绍了如何使用阿里巴巴的Fastjson库进行JSON数据的拼接和解析。通过添加Fastjson的依赖,我们可以利用其提供的API,如`JSON.toJSONString()`进行对象到JSON字符串的转换,使用`JSON.parseObject()`进行JSON字符串到对象的反序列化。文中还列举了其他常用的方法,如获取JSON对象中的数组、字符串、整数和布尔值等,并给出了实战练习,展示了如何创建对象并将其转换为JSON格式,以及如何解析JSON字符串获取特定字段的值。

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

JSON数据的拼接与解析

首先使用阿里的fastjson里的一个方法,导入fastjson包

在IDEA的pom文件中加入依赖

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

 常用方法API

1. 将对象序列化成json字符串

String com.alibaba.fastjson.JSON.toJSONString(Object object)

2. 将json字符串反序列化成对象

<T> Project com.alibaba.fastjson.JSON.parseObject(String text, Class<T> clazz)

3. 将json字符串反序列化成JSON对象

JSONObject com.alibaba.fastjson.JSON.parseObject(String text)

4.根据key 得到json中的json数组

JSONArray com.alibaba.fastjson.JSONObject.getJSONArray(String key)

5. 根据下标拿到json数组的json对象

JSONObject com.alibaba.fastjson.JSONArray.getJSONObject(int index)

6.. 根据key拿到json的字符串值

String com.alibaba.fastjson.JSONObject.getString(String key)

7. 根据key拿到json的int值

int com.alibaba.fastjson.JSONObject.getIntValue(String key)

8. 根据key拿到json的boolean值

boolean com.alibaba.fastjson.JSONObject.getBooleanValue(String key)

 

实战练习

创建对象 ResponseObject 

package com.srit.sxmarket.pojo.common;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResponseObject<T> {

    private Integer code;
    private String msg;
    private T data;

    public ResponseObject(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }
}

实验几个方法 

package com.srit.sxmarket.util.shiyan;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.srit.sxmarket.pojo.common.ResponseObject;

import java.util.*;
import java.util.stream.Collectors;

public class Test {
    public static void main(String args[]){
        mapLianxi();
    }
    public static void mapLianxi(){
        Map<String,Object> map=new HashMap<>();
        map.put("name","啦啦啦");
        map.put("sex","男");
        map.put("age","23");
        List listNew=new ArrayList();
        listNew.add(map);
        Map<String,Object> map2=new HashMap<>();
        map2.put("name","嘿嘿嘿");
        map2.put("sex","男");
        map2.put("age","23");
        listNew.add(map2);
        Map<String,Object> map3=new HashMap<>();
        map3.put("people",listNew);
        ResponseObject responseObject=new ResponseObject(1,"啦啦啦啦",map3);
        //传过来的json
        String json=JSONObject.toJSON(responseObject).toString();
        System.out.println("形成的json字符串内容为:"+json);
        //转成json对象
        JSONObject jsonObject=JSONObject.parseObject(json);
        //获取json的code和msg
        System.out.println("code "+jsonObject.getString("code")+" msg "+jsonObject.getString("msg"));
        //获取json的data对象内的对象信息
        JSONArray jsonArray=jsonObject.getJSONObject("data").getJSONArray("people");
        if(jsonArray.size()>0){
            for (int i=0;i<jsonArray.size();i++){
                System.out.println(jsonArray.get(i).toString());
            }
        }
    }
}

生成的json格式为

将对象拼接为Json格式代码:

ResponseObject responseObject=new ResponseObject(1,"啦啦啦啦",map3);
        //传过来的json
        String json=JSONObject.toJSON(responseObject).toString();
        System.out.println("形成的json字符串内容为:"+json);

转成将Json字符串解析成对象代码:

//转成json对象
        JSONObject jsonObject=JSONObject.parseObject(json);

获取Json字符串中code,msg,people中内容代码:

//获取json的code和msg
System.out.println("code "+jsonObject.getString("code")+" msg "+jsonObject.getString("msg"));
        //获取json的data对象内的对象信息
        JSONArray jsonArray=jsonObject.getJSONObject("data").getJSONArray("people");
        if(jsonArray.size()>0){
            for (int i=0;i<jsonArray.size();i++){
                System.out.println(jsonArray.get(i).toString());
            }
        }

例子2

HashMap<String,String>map = new HashMap<>();

map.put("a","1");

map.put("b","2");

map.put("c","3");

String json = JSON.toJSONString(map);//map转String

JSONObject jsonObject = JSON.parseObject(json);//String转jso

//json转map

 Map<String, String> jsonMap = JSONObject.toJavaObject(jsonObject, Map.class);

//String转map

 Map<String, String> jsonMap1 = JSONObject.parseObject(json, Map.class);

想了解更多详细也可参考此链接文章 https://segmentfault.com/a/1190000015363286

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值