SpringBoot 后端接收复杂JSON格式的参数

Json工具网站
https://www.bejson.com/json2javapojo/new
https://www.sojson.com/

Json字符串

{
“service”: {
“count”: “20”,
“item”: [
{
“id”: “EES_SS1830738”,
“option”: [
{
“description”: “答案”,
“id”: “1”
},
{
“description”: “答案2”,
“id”: “2”
},
{
“description”: “答案2”,
“id”: “3”
},
{
“description”: “建议书”,
“id”: “4”
}
],
“question”: “题目1”,
“rightanswer”: “3”,
“type”: “1”,
“value”: “5”
},
{
“id”: “EES_MS0654246”,
“option”: [
{
“description”: “12321321”,
“id”: “1”
},
{
“description”: "12321321 ",
“id”: “2”
},
{
“description”: “12321312”,
“id”: “3”
},
{
“description”: “12321”,
“id”: “4”
}
],
“question”: “题目”,
“rightanswer”: “1,3,4”,
“type”: “2”,
“value”: “5”
},
{
“id”: “EES_JU0561902”,
“option”: [
{
“description”: “是”,
“id”: “1”
},
{
“description”: “否”,
“id”: “0”
}
],
“question”: “12312312”,
“rightanswer”: “0”,
“type”: “3”,
“value”: “5”
}
]
}
}
在这里插入代码片

![在这里插入图片描述](https://img-blog.csdnimg.cn/20201203093914987.png)

## JsonRootBean
public class JsonRootBean {
	
	private Service service;
	
    public void setService(Service service) {
         this.service = service;
     }
     public Service getService() {
         return service;
     }
}

## Service

public class Service {
	
	private String count;
    private List<Item> item;
    public void setCount(String count) {
         this.count = count;
     }
     public String getCount() {
         return count;
     }

    public void setItem(List<Item> item) {
         this.item = item;
     }
     public List<Item> getItem() {
         return item;
     }
}

## Item
public class Item {
	private String id;
    private List<Option> option;
    private String question;
    private String rightanswer;
    private String type;
    private String value;

    //getter  
    //setter
}

## Option

public class Option {
	
	private String description;
    private String id;
    public void setDescription(String description) {
         this.description = description;
     }
     public String getDescription() {
         return description;
     }

    public void setId(String id) {
         this.id = id;
     }
     public String getId() {
         return id;
     }
}

## Controller
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.carlo.test.spboottest.bejson.JsonRootBean;
import com.carlo.test.spboottest.bejson.Service;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String index() {
        return "Hello World";
    }
    
    @PostMapping("/add")
    public String add(@RequestBody JsonRootBean jsonRootBean){
    	JsonRootBean jrb = new JsonRootBean();
    	jrb = jsonRootBean;
    	ObjectMapper mapper=new ObjectMapper();//定义一个转化对象
    	String json="";
		try {
			json = mapper.writeValueAsString(jrb);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
        System.out.println(json);
    	return json;
    }
    
    @PostMapping("/add1")
    public String add1(@RequestBody Service jsonRootBean){
    	Service jrb = new Service();
    	jrb = jsonRootBean;
    	ObjectMapper mapper=new ObjectMapper();//定义一个转化对象
    	String json="";
    	try {
    		json = mapper.writeValueAsString(jrb);
    	} catch (JsonProcessingException e) {
    		e.printStackTrace();
    	}
    	System.out.println(json);
    	return json;
    }

}

## POSMAN发送post请求
http://localhost:8080/add1

返回结果:
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201203094453371.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0NhcmxvUGFu,size_16,color_FFFFFF,t_70)

前端传入Json 不包括option时:
{
    "count": "20",
    "item": [
        {
            "id": "EES_SS1830738",
            
            "question": "题目1",
            "rightanswer": "3",
            "type": "1",
            "value": "5"
        },
        {
            "id": "EES_MS0654246",
            
            "question": "题目",
            "rightanswer": "1,3,4",
            "type": "2",
            "value": "5"
        },
        {
            "id": "EES_JU0561902",
           
            "question": "12312312",
            "rightanswer": "0",
            "type": "3",
            "value": "5"
        }
    ]
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/2020120310232921.png)
后端收到的结果,option为null
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201203102440679.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0NhcmxvUGFu,size_16,color_FFFFFF,t_70)


  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值