JSON格式字符串:
{
"errcode": 0,
"result": {
"list": [{
"approver_userid_list": ["1"],
"business_id": "201812101825000168225",
"create_time": "2018-12-10 18:25:17",
"form_component_values": [{
"name": "[\"开始时间\",\"结束时间\"]",
"value": "[\"2018-12-13 08:30\",\"2018-12-13 09:00\",0.5,\"hour\",\"事假\",\"请假类型\"]"
}, {
"name": "请假事由",
"value": "我们"
}, {
"name": "图片"
}],
"originator_dept_id": "90155004",
"originator_userid": "1",
"process_instance_id": "ab6c1d9b-46f9-4432-923f-1faf090f1184",
"process_instance_result": "",
"status": "RUNNING",
"title": "yang的请假1"
}, {
"approver_userid_list": ["1"],
"business_id": "201812101800000015509",
"create_time": "2018-12-10 18:00:02",
"finish_time": "2018-12-10 18:15:59",
"form_component_values": [{
"name": "[\"开始时间\",\"结束时间\"]",
"value": "[\"2018-12-10 22:00\",\"2018-12-12 22:00\",16,\"hour\",\"事假\",\"请假类型\"]"
}, {
"name": "请假事由",
"value": "没劲了"
}, {
"name": "图片"
}],
"originator_dept_id": "90155004",
"originator_userid": "1",
"process_instance_id": "4e041625-f61a-4cb3-b842-09785c13c69e",
"process_instance_result": "agree",
"status": "COMPLETED",
"title": "yang的请假"
}, {
"approver_userid_list": ["1"],
"business_id": "201812101708000039312",
"create_time": "2018-12-10 17:08:03",
"form_component_values": [{
"name": "[\"开始时间\",\"结束时间\"]",
"value": "[\"2018-12-04 18:00\",\"2018-12-10 17:07\",31.61,\"hour\",\"事假\",\"请假类型\"]"
}, {
"name": "请假事由",
"value": "咯"
}, {
"name": "图片"
}],
"originator_dept_id": "90155004",
"originator_userid": "1",
"process_instance_id": "0ef3ac47-0944-4182-a6d7-22cd2205755d",
"process_instance_result": "",
"status": "RUNNING",
"title": "yang的请假"
}]
},
"request_id": "88gkh7fjyd0o"
}
rsp.getBody()为字符串
JsonUtil.JsonFormart(rsp.getBody()):把字符串格式化为JSON字符串
JsonUtil.java类:
public class JsonUtil {
// public static void main(String[] args) {
// //json 字符串
// String s = "{\"dingtalk_smartwork_bpms_processinstance_list_response\":{\"result\":{\"ding_open_errcode\":0,\"result\":{\"list\":{\"process_instance_top_vo\":[{\"approver_userid_list\":{\"string\":[\"1\"]},\"business_id\":\"201812101825000168225\",\"create_time\":\"2018-12-10 18:25:17\",\"form_component_values\":{\"form_component_value_vo\":[{\"name\":\"[\\\"开始时间\\\",\\\"结束时间\\\"]\",\"value\":\"[\\\"2018-12-13 08:30\\\",\\\"2018-12-13 09:00\\\",0.5,\\\"hour\\\",\\\"事假\\\",\\\"请假类型\\\"]\"},{\"name\":\"请假事由\",\"value\":\"我们\"},{\"name\":\"图片\"}]},\"originator_dept_id\":\"90155004\",\"originator_userid\":\"1\",\"process_instance_id\":\"ab6c1d9b-46f9-4432-923f-1faf090f1184\",\"process_instance_result\":\"\",\"status\":\"RUNNING\",\"title\":\"yang的请假1\"},{\"approver_userid_list\":{\"string\":[\"1\"]},\"business_id\":\"201812101800000015509\",\"create_time\":\"2018-12-10 18:00:02\",\"finish_time\":\"2018-12-10 18:15:59\",\"form_component_values\":{\"form_component_value_vo\":[{\"name\":\"[\\\"开始时间\\\",\\\"结束时间\\\"]\",\"value\":\"[\\\"2018-12-10 22:00\\\",\\\"2018-12-12 22:00\\\",16,\\\"hour\\\",\\\"事假\\\",\\\"请假类型\\\"]\"},{\"name\":\"请假事由\",\"value\":\"没劲了\"},{\"name\":\"图片\"}]},\"originator_dept_id\":\"90155004\",\"originator_userid\":\"1\",\"process_instance_id\":\"4e041625-f61a-4cb3-b842-09785c13c69e\",\"process_instance_result\":\"agree\",\"status\":\"COMPLETED\",\"title\":\"yang的请假\"},{\"approver_userid_list\":{\"string\":[\"1\"]},\"business_id\":\"201812101708000039312\",\"create_time\":\"2018-12-10 17:08:03\",\"form_component_values\":{\"form_component_value_vo\":[{\"name\":\"[\\\"开始时间\\\",\\\"结束时间\\\"]\",\"value\":\"[\\\"2018-12-04 18:00\\\",\\\"2018-12-10 17:07\\\",31.61,\\\"hour\\\",\\\"事假\\\",\\\"请假类型\\\"]\"},{\"name\":\"请假事由\",\"value\":\"咯\"},{\"name\":\"图片\"}]},\"originator_dept_id\":\"90155004\",\"originator_userid\":\"1\",\"process_instance_id\":\"0ef3ac47-0944-4182-a6d7-22cd2205755d\",\"process_instance_result\":\"\",\"status\":\"RUNNING\",\"title\":\"yang的请假\"}]}},\"success\":true},\"request_id\":\"rswjfr3e2gcs\"}}";
//
// String out = JsonFormart(s);
// System.out.println(out);
//
// }
private static String getLevelStr(int level) {
StringBuffer levelStr = new StringBuffer();
for (int levelI = 0; levelI < level; levelI++) {
levelStr.append("\t");
}
return levelStr.toString();
}
public static String JsonFormart(String s) {
int level = 0;
//存放格式化的json字符串
StringBuffer jsonForMatStr = new StringBuffer();
for(int index=0;index<s.length();index++)//将字符串中的字符逐个按行输出
{
//获取s中的每个字符
char c = s.charAt(index);
// System.out.println(s.charAt(index));
//level大于0并且jsonForMatStr中的最后一个字符为\n,jsonForMatStr加入\t
if (level > 0 && '\n' == jsonForMatStr.charAt(jsonForMatStr.length() - 1)) {
jsonForMatStr.append(getLevelStr(level));
// System.out.println("123"+jsonForMatStr);
}
//遇到"{"和"["要增加空格和换行,遇到"}"和"]"要减少空格,以对应,遇到","要换行
switch (c) {
case '{':
case '[':
jsonForMatStr.append(c + "\n");
level++;
break;
case ',':
jsonForMatStr.append(c + "\n");
break;
case '}':
case ']':
jsonForMatStr.append("\n");
level--;
jsonForMatStr.append(getLevelStr(level));
jsonForMatStr.append(c);
break;
default:
jsonForMatStr.append(c);
break;
}
}
return jsonForMatStr.toString();
}
}
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
JSONObject jsonObject = JSONObject.parseObject(JsonUtil.JsonFormart(rsp.getBody()));
JSONObject jsonObject1 = jsonObject.getJSONObject("result"); // 取得result下的内容
System.out.println(jsonObject1);
JSONArray photoJsonArray = jsonObject1.getJSONArray("list"); // 取得list下的内容
System.out.println(photoJsonArray);
List<DocumentInfoEntity> document = new ArrayList<DocumentInfoEntity>();
DocumentInfoEntity d;
System.out.println(photoJsonArray.size());
for (int i = 0; i < photoJsonArray.size(); i++) { //list下的JSON数组内容进行循环
d = new DocumentInfoEntity(); //以下每循环一次创建一个新的对象,每次把 DocumentInfoEntity对象添加到List集合中
JSONObject jo = photoJsonArray.getJSONObject(i);
d.setDoctitle(jo.getString("title"));
d.setCometime(jo.getDate("create_time"));
JSONArray photoJsonArray1 = jo.getJSONArray("form_component_values");
for (int j = 0; j < 1; j++) {
JSONObject jo1 = photoJsonArray1.getJSONObject(j);
// System.out.println(jo1.getJSONArray("value").get(4));
d.setMajortype(jo1.getJSONArray("value").get(4).toString());
// System.out.println("xxxxxxxxxxx"+d);
document.add(d);
}
}
除了上面的JSON外,还有一种:
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
这种用法:
依赖:
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
将JSON格式字符串转换为JSON对象:JSONObject jsonp = JSONObject.fromObject(result);
将SON对象转换为JSON数组:JSONArray jsonArrayp = JSONArray.fromObject(jsonp);
for (int i = 0; i < jsonArrayp.size(); i++) {
JSONObject json = jsonArrayp.getJSONObject(i); // 遍历 jsonarray 数组,把每一个对象转成 json 对象
title=json.getString("title");
}