JAVA 解析複雜的json字串

Xxuexii發表於2018-07-20

原記錄在有道雲筆記。

public String pddGetOrders() {

String orderidinfo=pddMap();

System.out.println(orderidinfo);

Map<String, Object> results = JsonToMap.parseJSON2Map(orderidinfo);

Map<String, Object> order= (Map<String, Object>) results.get("order");

System.out.println(order);

String orderCount=String.valueOf(order.get("orderCount"));

int page=Integer.valueOf((String) order.get("page"));

System.out.println(orderCount);

System.out.println(page);

JSONArray array = JSONArray.fromObject(order.get("orderList"));

System.out.println(array);

//遍歷得到所有id,放進pddidslist集合裡

List<String> pddidslist=new ArrayList<String>();

do{

for (int i = 0; i < array.size(); i++) {

Map map= (Map) array.get(i);

String orderid=(String) map.get("orderSN");

System.out.println("訂單ID:"+orderid);

pddidslist.add(orderid);

System.out.println(pddidslist);

}

page++;

}

while(orderCount=="100");

// pddidslist轉換成陣列

String[] arrays = new String[pddidslist.size()];

for (int j = 0; j < pddidslist.size(); j++) {

arrays[j] = pddidslist.get(j);

}

List<Order> psidslist = orderService.selectPddOrders(arrays);

for(int k = 0; k < psidslist.size(); k++){

String psid=psidslist.get(k).getId();

for(int j = 0; j < pddidslist.size(); j++){

if (psid.equals(pddidslist.get(j))) {

//去掉資料庫中已有的

pddidslist.remove(j);

break;

}

}

}

System.out.println(pddidslist);

System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");

return "redirect:";

}

public String pddMap() {

Map<String, Object> order = new HashMap<String, Object>();

Map<String, Object> result = new HashMap<String, Object>();

Map<String, Object> item = new HashMap<String, Object>();

item.put("orderSN", "151210-65418251031");

Map<String, Object> item1 = new HashMap<String, Object>();

item1.put("orderSN", "151214-77466052010");

Map<String, Object> item2 = new HashMap<String, Object>();

item2.put("orderSN", "201612251706202020995");

Map<String, Object> item3 = new HashMap<String, Object>();

item3.put("orderSN", "201612251654555555680");

List<Map<String, Object>> orders = new ArrayList<Map<String, Object>>();

orders.add(item);

orders.add(item1);

orders.add(item2);

orders.add(item3);

result.put("orderCount", "4");

result.put("page", "1");

result.put("result", "1");

result.put("cause", "");

result.put("orderList", orders);

order.put("order", result);

String d=JSON.toJSONString(order);

return d;

}

  1. String parameter = {  
  2.   success : 0,  
  3.   errorMsg : "錯誤訊息",  
  4.   data : {  
  5.    total : "總記錄數",  
  6.    rows : [ {  
  7.     id : "任務ID",  
  8.     workName : "任務名稱",  
  9.     assigneeName : "經辦人姓名",  
  10.     name : "流程步驟名稱",  
  11.     processInstanceInitiatorName : "發起人",  
  12.     processInstanceStartTime : "發起時間",  
  13.     createTime : "到達時間",  
  14.     dueDate : "截止時間"  
  15.    }, {  
  16.     id : "ID",  
  17.     workName : "名稱",  
  18.     assigneeName : "經辦人",  
  19.     name : "流程",  
  20.     processInstanceInitiatorName : "發起人",  
  21.     processInstanceStartTime : "發起",  
  22.     createTime : "到達",  
  23.     dueDate : "截止"  
  24.    } ]  
  25.   }  
  26.  };  
    1. JSONObject jsonObject =JSONObject.fromObject(parameter);  
    2.    
    3. System.out.println("success:"+jsonObject.get("success"));  
    4.   System.out.println("errorMsg:"+jsonObject.get("errorMsg"));  
    5.     
    6.   System.err.println("total:"+jsonObject.getJSONObject("data").get("total"));  
    7.     
    8.   JSONObject data = jsonObject.getJSONObject("data");  
    9.   JSONArray jsonArray = data.getJSONArray("row");  
    10.   JSONObject row = null;  
    11.   for (int i = 0; i < jsonArray.size(); i++) {  
    12.    row = jsonArray.getJSONObject(i);  
    13.    System.out.println("id:"+row.get("id"));  
    14.    System.out.println("workName:"+row.get("workName"));  
    15.    System.out.println("assigneeName:"+row.get("assigneeName"));  
    16.    System.out.println("name:"+row.get("name"));  
    17.    System.out.println("processInstanceInitiatorName:"+row.get("processInstanceInitiatorName"));  
    18.    System.out.println("processInstanceStartTime:"+row.get("processInstanceStartTime"));  
    19.    System.out.println("createTime:"+row.get("createTime"));  
    20.    System.out.println("dueDate:"+row.get("dueDate"));  
    21.   } 

相關文章