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();