}
代码:
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; } }