解析Json格式字符串,Json与JavaBean互相转换

此文章使用fastJson 解析Json字符串,依赖如下:


	com.alibaba
	fastjson
	1.2.47

对于解析Json字符串,fastJson总共给出了三个类

  • JSON:fastJson的解析器,用于JSON格式字符串与JSON对象及javaBean之间的转换。
  • JSONObject:fastJson提供的json对象。
  • JSONArray:fastJson提供json数组对象

以下直接贴出测试代码,亲测无bug


package com.example.demo.jsonTest;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * @author lidai
 * @date 2018/10/30 9:54
 */
@SpringBootTest
@RunWith(SpringRunner.class)
@Slf4j
public class test {

    //json字符串--简单型
    private static final String  JSON_OBJ_STR = "{\"userId\":\"111\",\"username\":\"root\",\"password\":\"123456\"}";
    //json字符串--数组类型
    private static final String  JSON_ARRAY_STR = "[{\"userId\":\"111\",\"username\":\"root1\",\"password\":\"123456\"},{\"userId\":\"222\",\"username\":\"root2\",\"password\":\"654321\"}]";
    //json字符串--复杂类型
    private static final String  COMPLEX_JSON_STR = "{\"userId\":\"111\",\"username\":\"test\",\"password\":\"123456\",\"roleSet\":[{\"roleId\":\"gggg\",\"roleName\":\"common\",\"remark\":\"common\"},{\"roleId\":\"hfdkah\",\"roleName\":\"admin\",\"remark\":\"admin\"}]}";

    /**
     * Json字符串转为JsonObject
     */
    @Test
    public void JsonStringToJsonObject(){
        JSONObject jsonObject=JSONObject.parseObject(JSON_OBJ_STR);
        log.info("userId:{},username:{},password:{}",jsonObject.getString("userId"),jsonObject.get("username"),jsonObject.get("password"));
    }

    /**
     * Json字符串转为JsonArray
     */
    @Test
    public void JsonStringToJsonArray(){
        JSONArray jsonArray=JSONObject.parseArray(JSON_ARRAY_STR);
        for(Object obj:jsonArray){
            JSONObject jsonObject=(JSONObject)obj;
            log.info("userId:{},username:{},password:{}",jsonObject.getString("userId"),jsonObject.getString("username"),jsonObject.getString("password"));
        }
    }

    /**
     * Json字符串转为JsonObject,复杂类型
     */
    @Test
    public void JsonStringToJsonComplex(){
        JSONObject jsonObject=JSONObject.parseObject(COMPLEX_JSON_STR);
        log.info("userId:{}",jsonObject.getString("userId"));
        log.info("username:{}",jsonObject.getString("username"));
        log.info("password:{}",jsonObject.getString("password"));
        JSONArray roleSet=jsonObject.getJSONArray("roleSet");
        for(Object obj:roleSet){
            JSONObject object=(JSONObject)obj;
            log.info("roleId:{},roleName:{},remark:{}",object.getString("roleId"),object.getString("roleName"),object.getString("remark"));
        }
    }

    /**
     * Json字符串转为JavaBean
     */
    @Test
    public void JsonStringToJavaBean(){
        User user=JSONObject.parseObject(COMPLEX_JSON_STR,User.class);
        log.info("user:{}",user.toString());
    }

}

代码示例中最后一个测试需要User.java和Role.java,代码如下

User.java


package com.example.demo.entity;
import lombok.Data;
import lombok.ToString;


import javax.persistence.*;
import java.util.Set;

/**
 * @author lidai
 * @date 2018/10/23 13:53
 */
@Entity
@Data
@Table(name = "t_user")
@ToString
public class User {

    @Id
    private String userId;

    private String username;

    private String password;

    @ManyToMany(cascade = CascadeType.ALL, fetch=FetchType.EAGER)
    @JoinTable(name = "t_user_role",
            joinColumns = @JoinColumn(name = "user_id",referencedColumnName = "userId"),
            inverseJoinColumns = @JoinColumn(name = "role_id",referencedColumnName = "roleId"))
    private Set roleSet;

   
}

Role.java


package com.example.demo.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import lombok.ToString;

import javax.persistence.*;
import java.util.Set;

/**
 * @author lidai
 * @date 2018/10/29 14:15
 */
@Entity
@Table(name = "t_role")
@Data
@ToString
public class Role {

    @Id
    @GeneratedValue
    private String roleId;

    private String roleName;

    private String remark;

    @JsonIgnore
    @ManyToMany(fetch = FetchType.LAZY,mappedBy = "roleSet")
    private Set userSet;

}

 

你可能感兴趣的:(Json,Json,JavaBean,解析,转换)