目录
MockMvc测试,参数传递,前端通过ajax传递一个data,如果:
data是简单类,属性是int、string,并且不带嵌套,可以通过类的方式接收;
属性中有Long[],先接受int[],后端接收到之后再转化;
带有嵌套的复杂数据,则整体按照字符串接收,在后端解析。
controller接收方法,如果post传递的是json字符串,必须加@RequestBody
public AjaxResult addAll(@RequestBody String RiskString)
解析方式一:
坐标:
<properties>
<fastjson.version>1.2.68</fastjson.version>
</properties>
<!-- 阿里JSON解析器 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
引入包:
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;JSONObject jsonObject = JSON.parseObject(PgTotalStr);
解析:
//String PgTotalStr = "{\"pgbatchid\":\"批次\",\"inhossum\":\"1\",\"pathosid\":\"111111\",\"pgclass\":\"wai\",\"did\":1,\"patid\":1,\"subtotal\":[{\"pgtable \":\"table1\",\"pgResult\":\"出血高危\"},{\"pgtable \":\"table2\",\"pgResult\":\"出血低危\"}],\"total\":{\"count\":\"100\",\"grade\":\"等级\"},\"table1\":[{\"pgclass\":\"nei_chuxue\",\"tablename\":\"table1\",\"projectlist\":\"活动性消化道溃疡\",\"value\":\"10\",\"asScore\":\"10\",\"isSel\":\"true \"},{\"pgclass\":\"nei_chuxue\",\"tablename\":\"table1\",\"projectlist\":\"活动性消化道溃疡\",\"value\":\"10\",\"asScore\":\"10\",\"isSel\":\"true \"}],\"table2\":[{\"pgclass\":\"nei_chuxue\",\"tablename\":\"table2\",\"projectlist\":\"年龄≥85岁\",\"value\":\"10\",\"asScore\":\"10\",\"isSel\":\"true\"}]}";
JSONObject jsonObject = JSON.parseObject(PgTotalStr);
PgInparaSA pgInPara = new PgInparaSA();
pgInPara.setPgbatchid(jsonObject.getString("pgbatchid"));
类:
public class PgInparaSA {
private String pgbatchid;
...
private List<SubtotalS> subtotalS;
...
}
解析方式二:
引入包:
import net.sf.json.JSONObject;
import net.sf.json.JSONArray;
坐标:
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.3</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>net.sf.ezmorph</groupId>
<artifactId>ezmorph</artifactId>
<version>1.0.6</version>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.2.3</version>
<classifier>jdk15</classifier><!-- 指定jdk版本 -->
</dependency>
解析:
public List<BizChangeDetail> transToOb(String arrayStr)
{
//String arrayStr= "[{\"id\":\"1\",\"beforestr\":\"前1\",\"afterstr\":\"后1\",\"influence\":\"影响1\"},{\"beforestr\":\"前\",\"afterstr\":\"后\",\"influence\":\"影响\"}]" ;
JSONArray jsonArray=JSONArray.fromObject(arrayStr);
List<BizChangeDetail> bizChangeDetailList = new ArrayList<>();
// 回想数组的取值的方式? --->for循环遍历数组--->得到值
for (int i = 0; i < jsonArray.size(); i++) {
Object o=jsonArray.get( i );
JSONObject jsonObject=JSONObject.fromObject(o);
BizChangeDetail bizChangeDetail=(BizChangeDetail) JSONObject.toBean(jsonObject, BizChangeDetail.class );
bizChangeDetailList.add(bizChangeDetail);
}
return bizChangeDetailList;
}