1、Json数据格式模板样例
{
"RetVal":"1",
"TotalCount":"2",
"flag":"0",
"RetCode":"1",
"RowCount":"10",
"DataRows":[
[
{
"staff_name":"XXXX",
"ratio_list":"[{\"proj_name\":\"项目名称\",\"proj_inter_code\":\"YGI21ID0000002\",\"proj_type\":\"ZB\",\"proj_expend_expenses_type\":\"C研发项目\",\"proj_ratio\":\"1.00\",\"proj_percent\":\"100.00\",\"is_lock\":\"未确认\"},{\"proj_name\":\"公共项目\",\"proj_inter_code\":\"0000000000000\",\"proj_type\":\"\",\"proj_expend_expenses_type\":\"\",\"proj_ratio\":\"0.00\",\"proj_percent\":\"0.00\",\"is_lock\":\"未确认\"}]",
"staff_id":"0339975",
"is_lock":"未确认",
"status":"0"
},
{
"staff_name":"XXXX",
"ratio_list":"[{\"proj_name\":\"项目名称\",\"proj_inter_code\":\"YGI21UA0000007\",\"proj_type\":\"ZB\",\"proj_expend_expenses_type\":\"研发项目\",\"proj_ratio\":\"0.73\",\"proj_percent\":\"73.00\",\"is_lock\":\"未确认\"},{\"proj_name\":\"公共项目\",\"proj_inter_code\":\"0000000000000\",\"proj_type\":\"\",\"proj_expend_expenses_type\":\"\",\"proj_ratio\":\"0.27\",\"proj_percent\":\"27.00\",\"is_lock\":\"未确认\"}]",
"staff_id":"0340160",
"is_lock":"未确认",
"status":"0"
}
]
]
}
2、解析以上json数据单独获取字段“ratio_list” 字符串数组转换json格式循环读取数据
package timequery;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
public class test {
public static void main(String[] args) {
JSONObject Object = new JSONObject();
JSONObject jObjectRet = new JSONObject("{"RetVal":"1","TotalCount":"2","flag":"0","RetCode":"1","RowCount":"10","DataRows":[[{"staff_name":"XXXX","ratio_list":"[{\\\"proj_name\\\":\\\"项目名称\\\",\\\"proj_inter_code\\\":\\\"YGI21ID0000002\\\",\\\"proj_type\\\":\\\"ZB\\\",\\\"proj_expend_expenses_type\\\":\\\"研发项目\\\",\\\"proj_ratio\\\":\\\"1.00\\\",\\\"proj_percent\\\":\\\"100.00\\\",\\\"is_lock\\\":\\\"未确认\\\"},{\\\"proj_name\\\":\\\"公共项目\\\",\\\"proj_inter_code\\\":\\\"0000000000000\\\",\\\"proj_type\\\":\\\"\\\",\\\"proj_expend_expenses_type\\\":\\\"\\\",\\\"proj_ratio\\\":\\\"0.00\\\",\\\"proj_percent\\\":\\\"0.00\\\",\\\"is_lock\\\":\\\"未确认\\\"}]","staff_id":"0000","is_lock":"未确认","status":"0"},{"staff_name":"XXXX","ratio_list":"[{\\\"proj_name\\\":\\\"项目名称\\\",\\\"proj_inter_code\\\":\\\"YGI21UA0000007\\\",\\\"proj_type\\\":\\\"ZB\\\",\\\"proj_expend_expenses_type\\\":\\\"研发项目\\\",\\\"proj_ratio\\\":\\\"0.73\\\",\\\"proj_percent\\\":\\\"73.00\\\",\\\"is_lock\\\":\\\"未确认\\\"},{\\\"proj_name\\\":\\\"公共项目\\\",\\\"proj_inter_code\\\":\\\"0000000000000\\\",\\\"proj_type\\\":\\\"\\\",\\\"proj_expend_expenses_type\\\":\\\"\\\",\\\"proj_ratio\\\":\\\"0.27\\\",\\\"proj_percent\\\":\\\"27.00\\\",\\\"is_lock\\\":\\\"未确认\\\"}]","staff_id":"11111","is_lock":"未确认","status":"0"}]]}");
int num = 0;
if (jObjectRet.has("DataRows")) {
ArrayList array = new ArrayList();
JSONArray dataRows = jObjectRet.getJSONArray("DataRows");
for (int i = 0; i < dataRows.length(); i++) {
JSONObject rowsJSONObject = dataRows.getJSONObject(i);
String staffId = rowsJSONObject.getString("staff_id");
String staffName = rowsJSONObject.getString("staff_name");
String deptName = "";
if(rowsJSONObject.has("dept_name")) {
deptName = rowsJSONObject.getString("dept_name");
}
if(0!=rowsJSONObject.getString("ratio_list").length()) {
String ratio = rowsJSONObject.getString("ratio_list");
ratio = ratio.replaceAll("\\\\", "");
ratio = ratio.replaceAll("\"\\{", "\\{");
ratio = ratio.replaceAll("}\"", "\\}");
ratio = ratio.replaceAll("\"\\[", "\\[");
ratio = ratio.replaceAll("]\"", "\\]");
System.out.println(ratio);
JSONArray ratioList = new JSONArray(ratio);
for (int k = 0; k < ratioList.length(); k++) {
JSONObject rowsRatio = ratioList.getJSONObject(k);
timeExprotEntity entity = new timeExprotEntity();
String projName = rowsRatio.getString("proj_name");
String projInterCode = rowsRatio.getString("proj_inter_code");
String projType = rowsRatio.getString("proj_type");
String projExpendExpensesType = rowsRatio.getString("proj_expend_expenses_type");
String isLock = rowsRatio.getString("is_lock");
String projRatio = rowsRatio.getString("proj_ratio");
// 循环获取值存入list
entity.setNum(String.valueOf(++num));
entity.setStaffId(staffId);
entity.setStaffName(staffName);
entity.setDeptName(deptName);
entity.setProjName(projName);
entity.setProjInterCode(projInterCode);
entity.setProjType(projType);
entity.setProjExpendExpensesType(projExpendExpensesType);
entity.setIsLock(isLock);
entity.setProjRatio(projRatio);
array.add(entity);
}
}
}
jObjectRet.put("DataRows",array);
System.out.println(jObjectRet);
}
}
}
3、以上代码段中主要以转换数据并存入数据为主
针对字段 “ratio_list” 通过以下替换方式处理
String ratio = rowsJSONObject.getString("ratio_list");
ratio = ratio.replaceAll("\\\\", "");
ratio = ratio.replaceAll("\"\\{", "\\{");
ratio = ratio.replaceAll("}\"", "\\}");
ratio = ratio.replaceAll("\"\\[", "\\[");
ratio = ratio.replaceAll("]\"", "\\]");
System.out.println(ratio);
输出值为: