猿问

在Android中,我如何按日期对数组项进行分组?

对象array的



    Statement:[

       {

       "date": "12-09-19 11:02:47",

       "Country": "Bangladesh",

       "Profession": "X",

       "Salary": "100",

       },

       {

       "date": "12-09-19 11:02:47",

       "Country": "Bangladesh",

       "Profession": "Y",

       "Salary": "101",

       },

       {

       "date": "12-09-19 11:02:47",

       "Country": "Bangladesh",

       "Profession": "Z",

       "Salary": "102",

       },

       {

       "date": "11-09-19 11:02:47",

       "Country": "India",

       "Profession": "I",

       "Salary": "103",

       },

       {

       "date": "11-09-19 11:02:47",

       "Country": "India",

       "Profession": "J",

       "Salary": "104",

       },

       {

       "date": "10-09-19 11:02:47",

       "Country": "Nepal",

       "Profession": "N",

       "Salary": "105",

       },

       {

       "date": "10-09-19 11:02:47",

       "Country": "Nepal",

       "Profession": "M",

       "Salary": "106",

       }

       ]

我正在尝试创建一个新的array,它将创建一个object包含每个日期(作为键)和同一object.


新数组应如下所示:


    "10-09-19": [

    {

      "Country": "Nepal",

      "Profession": "M",

      "Salary": "106",

    },

    {

      "Country": "Nepal",

      "Profession": "N",

      "Salary": "105",

    }

    ],

    "11-09-19": [{

      "Country": "India",

      "Profession": "J",

       "Salary": "104",

    },

    {

      "Country": "India",

      "Profession": "I",

      "Salary": "103",

    }

    ],

    "12-09-19": [{

      "Country": "Bangladesh",

      "Profession": "x",

       "Salary": "104",

    },

    {

      "Country": "Bangladesh",

      "Profession": "y",

      "Salary": "103",

    },

    {

      "Country": "Bangladesh",

      "Profession": "z",

      "Salary": "102",

    }

    ]



婷婷同学_
浏览 123回答 4
4回答

肥皂起泡泡

使用 hashmap 明智地存储数据。哈希图的键将是日期。这是我的代码,它可能会帮助您理解逻辑。&nbsp; &nbsp; &nbsp; var sortedList= HashMap<String, MutableList<Model>>() // create hashmap to store data&nbsp; &nbsp; &nbsp; &nbsp; var temp: MutableList<Model>? = ArrayList<Model>()&nbsp; &nbsp; &nbsp; &nbsp; for (item in currentList!!) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp = sortedList?.get(item.date.split(" ").get(0)) // get date and remove timing&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (temp != null)&nbsp; &nbsp; &nbsp;//if this is not null it mean this contain items&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp.add(item)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp = ArrayList<Model>()&nbsp; //if this is null it means this is new data or new data&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp.add(item)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sortedList?.put(item.date.split(" ").get(0), temp)&nbsp; &nbsp; }我的代码是 kotlin 的,对此深表歉意。快乐编码。

慕村225694

我假设你提供的都是JSON字符串(但它们看起来无效),那么你可以按如下方式实现转换:ObjectMapper mapper = new ObjectMapper();ObjectNode root = (ObjectNode) mapper.readTree(jsonStr);ObjectNode rootNew = mapper.createObjectNode();for (int i = 0; i < root.get("Statement").size(); i++) {&nbsp; &nbsp; String date = root.get("Statement").get(i).get("date").asText().split(" ")[0];&nbsp; &nbsp; ObjectNode node = (ObjectNode) ((ObjectNode) root.get("Statement").get(i));&nbsp; &nbsp; node.remove("date");&nbsp; &nbsp; if (rootNew.has(date)) {&nbsp; &nbsp; &nbsp; &nbsp; ((ArrayNode) rootNew.get(date)).add(node);&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; rootNew.put(date, mapper.createArrayNode().add(node));&nbsp; &nbsp; }}System.out.println(rootNew.toString());控制台输出:{&nbsp; "12-09-19":[&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "Country":"Bangladesh",&nbsp; &nbsp; &nbsp; "Profession":"X",&nbsp; &nbsp; &nbsp; "Salary":"100"&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "Country":"Bangladesh",&nbsp; &nbsp; &nbsp; "Profession":"Y",&nbsp; &nbsp; &nbsp; "Salary":"101"&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "Country":"Bangladesh",&nbsp; &nbsp; &nbsp; "Profession":"Z",&nbsp; &nbsp; &nbsp; "Salary":"102"&nbsp; &nbsp; }&nbsp; ],&nbsp; "11-09-19":[&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "Country":"India",&nbsp; &nbsp; &nbsp; "Profession":"I",&nbsp; &nbsp; &nbsp; "Salary":"103"&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "Country":"India",&nbsp; &nbsp; &nbsp; "Profession":"J",&nbsp; &nbsp; &nbsp; "Salary":"104"&nbsp; &nbsp; }&nbsp; ],&nbsp; "10-09-19":[&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "Country":"Nepal",&nbsp; &nbsp; &nbsp; "Profession":"N",&nbsp; &nbsp; &nbsp; "Salary":"105"&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "Country":"Nepal",&nbsp; &nbsp; &nbsp; "Profession":"M",&nbsp; &nbsp; &nbsp; "Salary":"106"&nbsp; &nbsp; }&nbsp; ]}

拉丁的传说

首先感谢各位回答我的问题。我后来找到了一个解决方案。我想我应该与大家分享这一点。JSONArray statementArray = response.getJSONArray("statement");Map<String, List<String>> map= new HashMap<String, List<String>>();for(int i = 0; i < statementArray.length(); i ++) {&nbsp; &nbsp;JSONObject mJsonObjectStatement = statementArray.getJSONObject(i);&nbsp; &nbsp;String date= mJsonObjectStatement.getString("date").split(" ")[0];&nbsp; &nbsp;List<String> valSet = new ArrayList<String>();&nbsp; &nbsp;valSet.add(0,mJsonObjectStatement.getString("Country"));&nbsp; &nbsp;valSet.add(1,mJsonObjectStatement.getString("Profession"));&nbsp; &nbsp;valSet.add(2,mJsonObjectStatement.getString("Salary"));&nbsp; &nbsp;map.put(date,valSet);}for (Map.Entry<String, List<String>> entry : transactionReportMap.entrySet()) {&nbsp; &nbsp;String key = entry.getKey();&nbsp; &nbsp;List<String> values = entry.getValue();&nbsp; &nbsp;Log.e("Key = " , key);&nbsp; &nbsp;Log.e("Values = " , values + "n");}Log.e("Result:",map.toString());上述解决方案没有提供相同的结果,但它达到了我的目的。我找到的另一个解决方案JSONArray statementArray = response.getJSONArray("statement");ObjectMapper mapper = new ObjectMapper();Map<String, List<String>> map= new HashMap<String, List<String>>();for(int i = 0; i < statementArray.length(); i ++) {&nbsp; &nbsp;JSONObject mJsonObjectStatement = statementArray.getJSONObject(i);&nbsp; &nbsp;String date= mJsonObjectStatement.getString("date").split(" ")[0];&nbsp; &nbsp;if (transactionReportMap.containsKey(date)) {&nbsp; &nbsp; &nbsp; &nbsp;map.get(date).add(date);&nbsp; &nbsp;} else {&nbsp; &nbsp; &nbsp; &nbsp;ArrayList<String> infoList = new ArrayList<String>();&nbsp; &nbsp; &nbsp; &nbsp;infoList.add(mJsonObjectStatement.getString("Country"));&nbsp; &nbsp; &nbsp; &nbsp;infoList.add(mJsonObjectStatement.getString("Profession"));&nbsp; &nbsp; &nbsp; &nbsp;infoList.add(mJsonObjectStatement.getString("Salary"));&nbsp; &nbsp; &nbsp; &nbsp;map.put(date, infoList);&nbsp; &nbsp;}}StringWriter result = new StringWriter();try {&nbsp; mapper.writeValue(result, map);} catch (IOException e) {&nbsp; e.printStackTrace();}Log.e("Result: ",result+"");

开心每一天1111

对于这个问题来说可能为时已晚,但我正在分享我的答案。&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Define an empty hash map&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Map<String, JSONObject> finalMap = map;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (JSONObject object : Statement) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String key = object.getString("date");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Split the date from date time&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (key.contains(" ")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; key = key.split(" ")[0];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (finalMap.containsKey(key)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONArray list = finalMap.get(key);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.put(student);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONArray list = new JSONArray();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.put(object);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; finalMap.put(key, list);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }最后,您可以将哈希映射转换为您需要的任何类型。
随时随地看视频慕课网APP

相关分类

Java
我要回答