运行结果如图:
代码:
PRivate void postJsonData() { List<OrgIds> orgsList = new ArrayList<OrgIds>();//自己写的list做假数据 OrgIds org = null; for (int k = 0 ; k < 3 ;k++){//list里添加数据 org = new OrgIds(); org.setOrgId(39021+""+k); orgsList.add(org); } JSONArray orgArray = new JSONArray(); JSONObject orgspParent = new JSONObject(); JSONObject tmpObj = null; JSONArray siteArray =null;//siteType 的jsonArray int count = orgsList.size(); for(int i = 0; i < count; i++) { tmpObj = new JSONObject(); try { tmpObj.put("orgId" , orgsList.get(i).getOrgId()); } catch (JSONException e) { e.printStackTrace(); } siteArray = new JSONArray(); for (int j = 0; j < 3 ; j++) {//往 siteType里添加数据 这里写死了 siteArray.put("餐饮"); try { tmpObj.put("siteType", siteArray);//把siteType的JsonArray添加进来 } catch (JSONException e) { e.printStackTrace(); } } orgArray.put(tmpObj); tmpObj = null; } try { String str = orgspParent.put("orgs" , orgArray).toString().replaceAll("////",""); // 获得JSONObject的String// Log.e(TAG, "postJsonData: "+str);// tv.setText(str); } catch (Exception e) { e.printStackTrace(); } }
新闻热点
疑难解答