json

xiaoxiao2021-02-27  347

{     "flag": true,     "msg": "成功",     "data": {         "orders": {             "2017-05-05": [                 {                     "PRICE": 5.44,                     "ORDER_STATUS": 1,                     "CUSTOMER_NAME": "黎叔",                     "GASSTATION_ID": 15,                     "CAR_ID": 17,                     "COMPANY_ID": 15,                     "COMPANY_NAME": "测试物流公司",                     "ORDER_NO": "1493805638549",                     "CREATE_USER_ID": 1,                     "ORD_DATE": "2017-05-05",                     "CUSTOMER_ID": 1,                     "CAR_NUM": "京A10001",                     "STAFF_ID": 1,                     "TEAM_ID": 17,                     "CREATE_USER_NAME": "1",                     "DATE_KEY": {                         "nanos": 0,                         "time": 1493913600000,                         "minutes": 0,                         "seconds": 0,                         "hours": 0,                         "month": 4,                         "year": 117,                         "timezoneOffset": -480,                         "day": 5,                         "date": 5                     },                     "ID": 29,                     "QTY": 5,                     "TOTAL_PRICE": 27.2,                     "STAFF_NAME": "1",                     "TEAM_NAME": "测试车队一",                     "CREATE_DATE": {                         "nanos": 0,                         "time": 1493805639000,                         "minutes": 0,                         "seconds": 39,                         "hours": 18,                         "month": 4,                         "year": 117,                         "timezoneOffset": -480,                         "day": 3,                         "date": 3                     },                     "RECEIPT_URL": "1493805647128.jpg"                 }             ],             "2017-05-03": [                 {                     "PRICE": 5.44,                     "ORDER_STATUS": 1,                     "CUSTOMER_NAME": "黎叔",                     "GASSTATION_ID": 15,                     "CAR_ID": 17,                     "COMPANY_ID": 15,                     "COMPANY_NAME": "测试物流公司",                     "ORDER_NO": "1493805059061",                     "CREATE_USER_ID": 1,                     "ORD_DATE": "2017-05-03",                     "CUSTOMER_ID": 1,                     "CAR_NUM": "京A10001",                     "STAFF_ID": 1,                     "TEAM_ID": 17,                     "CREATE_USER_NAME": "1",                     "DATE_KEY": {                         "nanos": 0,                         "time": 1493805059000,                         "minutes": 50,                         "seconds": 59,                         "hours": 17,                         "month": 4,                         "year": 117,                         "timezoneOffset": -480,                         "day": 3,                         "date": 3                     },                     "ID": 28,                     "QTY": 2,                     "TOTAL_PRICE": 10.88,                     "STAFF_NAME": "1",                     "TEAM_NAME": "测试车队一",                     "CREATE_DATE": {                         "nanos": 0,                         "time": 1493805059000,                         "minutes": 50,                         "seconds": 59,                         "hours": 17,                         "month": 4,                         "year": 117,                         "timezoneOffset": -480,                         "day": 3,                         "date": 3                     },                     "RECEIPT_URL": "1493806771838.jpg"                 },                 {                     "PRICE": 5.44,                     "ORDER_STATUS": 1,                     "CUSTOMER_NAME": "黎叔",                     "GASSTATION_ID": 15,                     "CAR_ID": 17,                     "COMPANY_ID": 15,                     "COMPANY_NAME": "测试物流公司",                     "ORDER_NO": "1493805828238",                     "CREATE_USER_ID": 1,                     "ORD_DATE": "2017-05-03",                     "CUSTOMER_ID": 1,                     "CAR_NUM": "京A10001",                     "STAFF_ID": 1,                     "TEAM_ID": 17,                     "CREATE_USER_NAME": "1",                     "DATE_KEY": {                         "nanos": 0,                         "time": 1493740800000,                         "minutes": 0,                         "seconds": 0,                         "hours": 0,                         "month": 4,                         "year": 117,                         "timezoneOffset": -480,                         "day": 3,                         "date": 3                     },                     "ID": 30,                     "QTY": 8,                     "TOTAL_PRICE": 43.52,                     "STAFF_NAME": "1",                     "TEAM_NAME": "测试车队一",                     "CREATE_DATE": {                         "nanos": 0,                         "time": 1493805828000,                         "minutes": 3,                         "seconds": 48,                         "hours": 18,                         "month": 4,                         "year": 117,                         "timezoneOffset": -480,                         "day": 3,                         "date": 3                     },                     "RECEIPT_URL": "1493806823526.jpg"                 }             ]         }     }

}

代码:

CommonJSONParser commonJSONParser = new CommonJSONParser(); Map<String, Object> result = commonJSONParser.parse(str); for (Map.Entry<String, Object> entry : result.entrySet()) { System.out.println("key= " + entry.getKey()); } CommonJSONParser commonJSONParser1 = new CommonJSONParser(); Map<String, Object> result1 = commonJSONParser1.parse(result.get("data").toString()); for (Map.Entry<String, Object> entry : result1.entrySet()) { System.out.println("key1= " + entry.getKey()); } CommonJSONParser commonJSONParser2 = new CommonJSONParser(); Map<String, Object> result2 = commonJSONParser2.parse(result1.get("orders").toString()); for (Map.Entry<String, Object> entry : result2.entrySet()) { System.out.println("key2= " + entry.getKey()); System.out.println("........."+entry.getValue()); } // int num = result.length(); JSONArray nameList = null;//获取JSONArray try { JSONObject result3 = new JSONObject(result1.get("orders").toString());//转换为JSONObject nameList = result3.getJSONArray("2017-05-03"); int length = nameList.length(); String aa = ""; for(int i = 0; i < length; i++) {//遍历JSONArray JSONObject oj = null; try { oj = nameList.getJSONObject(i); aa = aa + oj.getString("CUSTOMER_NAME")+"|"; Log.d("debugTest",Integer.toString(i)+" aa-- "+aa); System.out.println("......debugTest1111........"+Integer.toString(i)+" aa-- "+aa); } catch (JSONException e) { e.printStackTrace(); } } // Iterator<?> it = result3.keys(); // String aa2 = ""; // String bb2 = null; // while(it.hasNext()){//遍历JSONObject // bb2 = (String) it.next().toString(); // try { // aa2 = aa2 + result3.getString(bb2); // System.out.println("....debugTest2222....."+bb2+" -- "+aa2); // } catch (JSONException e) { // e.printStackTrace(); // } // // } } catch (JSONException e) { e.printStackTrace(); }

帮助类:

import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; /** * Created by lys13 on 2017/5/4. */ public class CommonJSONParser { public Map<String, Object> parse(String jsonStr) { Map<String, Object> result = null; if (null != jsonStr) { try { JSONObject jsonObject = new JSONObject(jsonStr); result = parseJSONObject(jsonObject); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // if (null != jsonStr) return result; } private Object parseValue(Object inputObject) throws JSONException { Object outputObject = null; if (null != inputObject) { if (inputObject instanceof JSONArray) { outputObject = parseJSONArray((JSONArray) inputObject); } else if (inputObject instanceof JSONObject) { outputObject = parseJSONObject((JSONObject) inputObject); } else if (inputObject instanceof String || inputObject instanceof Boolean || inputObject instanceof Integer) { outputObject = inputObject; } } return outputObject; } private List<Object> parseJSONArray(JSONArray jsonArray) throws JSONException { List<Object> valueList = null; if (null != jsonArray) { valueList = new ArrayList<Object>(); for (int i = 0; i < jsonArray.length(); i++) { Object itemObject = jsonArray.get(i); if (null != itemObject) { valueList.add(parseValue(itemObject)); } } // for (int i = 0; i < jsonArray.length(); i++) } // if (null != valueStr) return valueList; } private Map<String, Object> parseJSONObject(JSONObject jsonObject) throws JSONException { Map<String, Object> valueObject = null; if (null != jsonObject) { valueObject = new HashMap<String, Object>(); Iterator<String> keyIter = jsonObject.keys(); while (keyIter.hasNext()) { String keyStr = keyIter.next(); Object itemObject = jsonObject.opt(keyStr); if (null != itemObject) { valueObject.put(keyStr, parseValue(itemObject)); } // if (null != itemValueStr) } // while (keyIter.hasNext()) } // if (null != valueStr) return valueObject; } }

转载请注明原文地址: https://www.6miu.com/read-2231.html

最新回复(0)