1.JSONObject转String
JSONObject result = new JSONObject();
String str= JSON.toJSONString(result);
2.List转JSONArray
List<T> list = new ArrayList<T>();
JSONArray array= JSONArray.parseArray(JSON.toJSONString(list));
3.JSONArray转List
JSONArray array = new JSONArray();
List<EventColAttr> list = JSONObject.parseArray(array.toJSONString(), EventColAttr.class);
4.String转JSONArray
String st = "[{name:Tim,age:25,sex:male},{name:Tom,age:28,sex:male},{name:Lily,age:15,sex:female}]";
JSONArray tableData = JSONArray.parseArray(st);
5.JsonArray->JsonObject
JSONObject result = new JSONObject();
JSONArray array = new JSONArray();
result.put("data",jsonArray);
代码整合
//获取一个List
List<SelfPosJson> positionByUserId = selfLocationService.getPositionByUserId(userId,coordStatus);
//创建一个JSONObject
JSONObject result = new JSONObject();
//创建一个JSONArray
JSONArray jsonArray= JSONArray.parseArray(JSON.toJSONString(positionByUserId));
//把JSONArray 放进JSONObject
result.put("data",jsonArray);