当业务需要请求其他系统的接口时,通过httpClient等工具类,传参需要把对象转成json,或者formData数据。
alibaba.fastjson包中的JSON , JSONObject ,JSONArray 处理相互转换的关系
1.对象转json (懒得new对象的时候可以用map
import com.alibaba.fastjson.JSON;
Map<String,Object> map=new HashMap<>();
map.put("key","value");
String jsons = JSON.toJSONString(map);
获取到请求结果的时候
2.json转对象。一般返回结果的类型{“data”:object,"msg":"ok","status":200}
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
String result="{'data[{'ChargeDate':'/Date(1543287487000+0800)/','Amount':123.00}]
,'msg':'ok','status':1}";
JSONObject jsonObject = JSONObject.parseObject(result);
String msg=jsonObject.getString("msg");
//data对应的数据时个数组&#