springb2.0.4中JSONObject和JSONArray的使用

版权声明:不卑不亢,不骄不躁---好男人就是我,我就是曾哥. https://blog.csdn.net/weixin_42884584/article/details/82387545

先引入jar包:

<!--处理JSON格式-->
<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>fastjson</artifactId>
	<version>1.2.3</version>
</dependency>

JSONObject用法:可以接收的数据格式:{ "id" : "123", "courseID" : "huangt-test", "title" : "提交作业", "content" : null  } 

JSONObject jsonObject = JSON.parseObject(string);
String id = jsonObject.getString("id");
String data = jsonObject.getString("title");

JSONArray,顾名思义是由JSONObject构成的数组,用  [ { } , { } , ......  , { } ]  来表示

例如:   [ {  "id" : "123", "courseID" : "huangt-test", "title" : "提交作业" }  ,  {  "content" : null, "beginTime" : 1398873600000  "endTime" } ] 

JSONArray jsonArray=JSON.parseArray(string);
for(int i=0;i<jsonArray.size();i++){
    JSONObject   jsonObject  =  jsonArray.getJSONObject(i) ;
    String id = jsonObject.getString("id");
    String data = jsonObject.getString("title");
}

还有用法以后再补充。

猜你喜欢

转载自blog.csdn.net/weixin_42884584/article/details/82387545