Hello,大家好!我是程序員阿飛!今天主要給大家分享一下Java中Map、JSONObject、JSONArray之間的轉換,好了,我們直奔主題吧!
1、Map轉JSONObject對象
JSONObject jsonObject = JSONObject.fromObject(params);//import net.sf.json.JSONObject;
2、Map轉JSONArray數組
JSONArray jsonArray= JSONArray.fromObject(paramsMap);//import net.sf.json.JSONArray;
3、獲取JSONObject的值
String taskCode = jsonObject.get("taskCode").toString();
String code = jsonObject.getString("code");
4、Json字符串轉JSONObject對象
JSONObject jsonObject = JSONObject.fromObject(result);
5、判斷JSONObject是否爲空
!jsonObject.isNullObject()
6、Json字符串轉JSONArray數組
JSONArray data = parseArray(datas.get("data").toString());
7、一個項目請求另一個項目的方法
result = HttpRequest.post(newUrl).timeout(3000).form(params).execute().body();