地域统计 测试
package com.sq.test; import org.json.JSONArray; import org.json.JSONObject; import com.sq.utilities.Constants; import com.sq.utilities.JSONArrayProcessHelper; /** * <pre> *----the day before yesterday----------------------------- * 北京 1 123 * 上海 3 101 *----yesterday---------------------------- * 北京 5 ? * 无锡 1 ? * 常州 3 ? *----statics yesterday---------------------------- *北京 5 128 *上海 0 101 *无锡 1 1 *常州 3 3 * </pre> * @author baoy * */ public class UserAreaJsonTest { final static String DATA_STRING = "data"; final static String ALL_USER_COUNT = "all_user_count"; final static String NEW_USER_COUNT = "new_user_count"; final static String USER_AREA_CITY = "user_area_city"; public static void main(String[] args) throws Exception { JSONObject yesterday = yesterday(); JSONObject newuser = newuser(); JSONArray yesterdayJsonArray= yesterday.getJSONArray(DATA_STRING); JSONArray todaydayJsonArray = newuser.getJSONArray(DATA_STRING); JSONObject resultJson = new JSONObject(); JSONArray jsonArray = new JSONArray(); getAllSet(yesterdayJsonArray, todaydayJsonArray); System.out.println("-----------------------------"); printLog(yesterdayJsonArray); getJSONArray(jsonArray, yesterdayJsonArray, todaydayJsonArray); JSONArray jsonArray2 = JSONArrayProcessHelper.sortJSONArray(jsonArray,ALL_USER_COUNT ); resultJson.put(DATA_STRING, jsonArray2); System.out.println("-----------------------------"); printLog(resultJson); } private static void getJSONArray (JSONArray jsonArray, JSONArray yesterdayJsonArray,JSONArray todaydayJsonArray) throws Exception { for (int i = 0; i < yesterdayJsonArray.length(); i++) { JSONObject jObj = yesterdayJsonArray.getJSONObject(i); boolean isSelected = false; for (int j = 0; j < todaydayJsonArray.length(); j++) { if ( !isSelected && todaydayJsonArray.getJSONObject(j).getString(USER_AREA_CITY) .equals(jObj.getString(USER_AREA_CITY)) ) { JSONObject oneObject = new JSONObject(); oneObject.put(NEW_USER_COUNT , todaydayJsonArray.getJSONObject(j).getLong(NEW_USER_COUNT) ); oneObject.put(USER_AREA_CITY, todaydayJsonArray.getJSONObject(j).getString(USER_AREA_CITY)); oneObject.put(ALL_USER_COUNT, todaydayJsonArray.getJSONObject(j).getLong(NEW_USER_COUNT) + jObj.getLong(ALL_USER_COUNT) ); jsonArray.put(oneObject); isSelected = true; } } if (isSelected== false) { JSONObject oneObject = new JSONObject(); oneObject.put(NEW_USER_COUNT , 0 ); oneObject.put(USER_AREA_CITY, jObj.getString(USER_AREA_CITY)); oneObject.put(ALL_USER_COUNT,jObj.getLong(ALL_USER_COUNT) ); jsonArray.put(oneObject); isSelected = true; } } } private static void getAllSet(JSONArray yesterdayJsonArray,JSONArray todaydayJsonArray) throws Exception{ for (int i = 0; i < todaydayJsonArray.length(); i++) { JSONObject Jobj= todaydayJsonArray.getJSONObject(i); boolean isSelected = false; for ( int j = 0; j < yesterdayJsonArray.length(); j++) { if(!isSelected && yesterdayJsonArray.getJSONObject(j).getString(USER_AREA_CITY).equals(Jobj.getString(USER_AREA_CITY))) { isSelected = true; }else if (!isSelected && j==yesterdayJsonArray.length()-1 ) { isSelected = true; JSONObject oneObject = new JSONObject(); oneObject.put(NEW_USER_COUNT , 0); oneObject.put(USER_AREA_CITY, Jobj.getString(USER_AREA_CITY)); oneObject.put(ALL_USER_COUNT, 0); yesterdayJsonArray.put(oneObject); } } if (isSelected ==false) { isSelected = true; JSONObject oneObject = new JSONObject(); oneObject.put(NEW_USER_COUNT , 0); oneObject.put(USER_AREA_CITY, Jobj.getString(USER_AREA_CITY)); oneObject.put(ALL_USER_COUNT, 0); yesterdayJsonArray.put(oneObject); } } } private static void printLog (JSONArray jsonArray) throws Exception{ for (int i = 0; i < jsonArray.length(); i++) { JSONObject jObj = jsonArray.getJSONObject(i); System.out.println( jObj.getString(USER_AREA_CITY)+" "+ jObj.getLong(NEW_USER_COUNT)+" "+ jObj.getLong(ALL_USER_COUNT) ); } } private static void printLog (JSONObject resultJson) throws Exception{ JSONArray yesterdayJsonArray = resultJson.getJSONArray(DATA_STRING); printLog(yesterdayJsonArray); } private static JSONObject yesterday(){ JSONObject resultJson = new JSONObject(); JSONArray jsonArray = new JSONArray(); try { resultJson.put(DATA_STRING, jsonArray); JSONObject oneObject = new JSONObject(); oneObject.put(NEW_USER_COUNT , 1); oneObject.put(USER_AREA_CITY, "北京"); oneObject.put(ALL_USER_COUNT, 123); jsonArray.put(oneObject); JSONObject twoObject = new JSONObject(); twoObject.put(NEW_USER_COUNT , 3); twoObject.put(USER_AREA_CITY, "上海"); twoObject.put(ALL_USER_COUNT, 101); jsonArray.put(twoObject); return resultJson; }catch (Exception e){ return resultJson; } } private static JSONObject newuser(){ JSONObject resultJson = new JSONObject(); JSONArray jsonArray = new JSONArray(); try { resultJson.put(DATA_STRING, jsonArray); JSONObject oneObject = new JSONObject(); oneObject.put(NEW_USER_COUNT , 1); oneObject.put(USER_AREA_CITY, "无锡"); jsonArray.put(oneObject); JSONObject twoObject = new JSONObject(); twoObject.put(NEW_USER_COUNT , 3); twoObject.put(USER_AREA_CITY, "常州"); jsonArray.put(twoObject); JSONObject threeObject = new JSONObject(); threeObject.put(NEW_USER_COUNT , 5); threeObject.put(USER_AREA_CITY, "北京"); jsonArray.put(threeObject); return resultJson; }catch (Exception e){ return resultJson; } } }
json 排序
package com.sq.utilities; import org.apache.log4j.Logger; import org.json.JSONArray; import org.json.JSONObject; public class JSONArrayProcessHelper { private static Logger log = Logger.getLogger(JSONArrayProcessHelper.class); /** * @param data 需要进行排序的数据 * @param tag 对数据的哪个维度进行排序 * @param index 输出的排序结果的最长长度,-1表示全部 * @return */ public static JSONArray sortJSONArray(JSONArray data, String tag) { if (data == null || tag == null) { log.error("Sort JSONArray param is null "); return null; } JSONArray dataArray = data; try { log.info("begin sort data length is : " + data.length()); for (int i = 0; i < dataArray.length(); i++) { JSONObject teI = dataArray.getJSONObject(i); long valueI = teI.getLong(tag); for (int j = i; j < dataArray.length(); j++) { JSONObject teJ = dataArray.getJSONObject(j); long valueJ = teJ.getLong(tag); if (valueJ > valueI) { // swap JSONObject temp = new JSONObject(); temp = dataArray.getJSONObject(i); dataArray.put(i, dataArray.get(j)); dataArray.put(j, temp); valueI = valueJ; // !! } } } } catch (Exception e) { e.printStackTrace(); return null; } return dataArray; } }
捐助开发者
在兴趣的驱动下,写一个免费
的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。
谢谢您的赞助,我会做的更好!