/*****************************先准备json数据*****************************/
//这个json用来转List<T>
static String jsonList="{\r\n" +
" \"code\": \"0\",\r\n" +
" \"msg\": \"排号列表查询成功\",\r\n" +
" \"data\": [\r\n" +
" {\r\n" +
" \"consultationRoom\": \"209室\",\r\n" +
" \"rowNumber\": \"1\",\r\n" +
" \"patient\": \"陈*生\"\r\n" +
" },\r\n" +
" {\r\n" +
" \"consultationRoom\": \"209室\",\r\n" +
" \"rowNumber\": \"2\",\r\n" +
" \"patient\": \"陈*莲\"\r\n" +
" },\r\n" +
" {\r\n" +
" \"consultationRoom\": \"209室\",\r\n" +
" \"rowNumber\": \"3\",\r\n" +
" \"patient\": \"陈*气\"\r\n" +
" },\r\n" +
" {\r\n" +
" \"consultationRoom\": \"209室\",\r\n" +
" \"rowNumber\": \"4\",\r\n" +
" \"patient\": \"*飞\"\r\n" +
" }\r\n" +
" ]\r\n" +
"}";
static org.json.JSONObject json= new JSONObject(jsonList);
//这个json用来转Map
static String jsonMap="{\r\n" +
" \"msg\": \"排号成功\",\r\n" +
" \"code\": \"0\",\r\n" +
" \"data\": \r\n" +
" {\r\n" +
" \"doctorNo\": \"798\",\r\n" +
" \"patient\": \"陈*生\",\r\n" +
" \"doctorPhotoPath\": \"https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3347806870,81394400&fm=26&gp=0.jpg\",\r\n" +
" \"consultationRoom\": \"209室\",\r\n" +
" \"departmentName\": \"内伤一部\",\r\n" +
" \"departmentNo\": \"4324324\",\r\n" +
" \"doctorName\": \"黄裳\",\r\n" +
" \"rowNumber\": \"1\",\r\n" +
" \"rowNumberType\": \"1\",\r\n" +
" \"subjectName\": \"内伤一科\",\r\n" +
" \"subjectNo\": \"43242\",\r\n" +
" \"date\": \"2020-02-19-AM\",\r\n" +
" \"orderNo\": \"980698580095736422542\"\r\n" +
" }\r\n" +
"}";
static JSONObject json2= new JSONObject(jsonMap);
/*******************************这个对象下面要用*******************************/
com.google.gson.Gson gson = new Gson();
/***************************JSON转List<Map<String,Object>>***************************/
org.json.JSONObject jsonObject = Demo.json;
String dataString = jsonObject.get("data").toString();
List<Map<String, Object>> list = gson.fromJson(dataString, new com.google.common.reflect.TypeToken<List<Map<String, Object>>>() {
}.getType());
/***************************JSON转List<Bean>***************************/
org.json.JSONObject jsonObject2 = Demo.json;
String dataString = jsonObject2.get("data").toString();
List<Bean> list2 = gson.fromJson(dataString, new com.google.common.reflect.TypeToken<List<Bean>>() {
}.getType());
/***************************JSON转Map<String,Object>***************************/
org.json.JSONObject jsonObject3 = Demo.json2;
String dataString = jsonObject3.get("data").toString();
Map<String, Object> m = gson.fromJson(dataString, new TypeToken<Map<String, Object>>() {
}.getType());
相关文案
String和JSON互相转换