4 回答
TA贡献1829条经验 获得超6个赞
使用 hashmap 明智地存储数据。哈希图的键将是日期。这是我的代码,它可能会帮助您理解逻辑。
var sortedList= HashMap<String, MutableList<Model>>() // create hashmap to store data
var temp: MutableList<Model>? = ArrayList<Model>()
for (item in currentList!!) {
temp = sortedList?.get(item.date.split(" ").get(0)) // get date and remove timing
if (temp != null) //if this is not null it mean this contain items
temp.add(item)
else {
temp = ArrayList<Model>() //if this is null it means this is new data or new data
temp.add(item)
}
sortedList?.put(item.date.split(" ").get(0), temp)
}
我的代码是 kotlin 的,对此深表歉意。快乐编码。
TA贡献1880条经验 获得超4个赞
我假设你提供的都是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++) {
String date = root.get("Statement").get(i).get("date").asText().split(" ")[0];
ObjectNode node = (ObjectNode) ((ObjectNode) root.get("Statement").get(i));
node.remove("date");
if (rootNew.has(date)) {
((ArrayNode) rootNew.get(date)).add(node);
} else {
rootNew.put(date, mapper.createArrayNode().add(node));
}
}
System.out.println(rootNew.toString());
控制台输出:
{
"12-09-19":[
{
"Country":"Bangladesh",
"Profession":"X",
"Salary":"100"
},
{
"Country":"Bangladesh",
"Profession":"Y",
"Salary":"101"
},
{
"Country":"Bangladesh",
"Profession":"Z",
"Salary":"102"
}
],
"11-09-19":[
{
"Country":"India",
"Profession":"I",
"Salary":"103"
},
{
"Country":"India",
"Profession":"J",
"Salary":"104"
}
],
"10-09-19":[
{
"Country":"Nepal",
"Profession":"N",
"Salary":"105"
},
{
"Country":"Nepal",
"Profession":"M",
"Salary":"106"
}
]
}
TA贡献1789条经验 获得超8个赞
首先感谢各位回答我的问题。我后来找到了一个解决方案。我想我应该与大家分享这一点。
JSONArray statementArray = response.getJSONArray("statement");
Map<String, List<String>> map= new HashMap<String, List<String>>();
for(int i = 0; i < statementArray.length(); i ++) {
JSONObject mJsonObjectStatement = statementArray.getJSONObject(i);
String date= mJsonObjectStatement.getString("date").split(" ")[0];
List<String> valSet = new ArrayList<String>();
valSet.add(0,mJsonObjectStatement.getString("Country"));
valSet.add(1,mJsonObjectStatement.getString("Profession"));
valSet.add(2,mJsonObjectStatement.getString("Salary"));
map.put(date,valSet);
}
for (Map.Entry<String, List<String>> entry : transactionReportMap.entrySet()) {
String key = entry.getKey();
List<String> values = entry.getValue();
Log.e("Key = " , key);
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 ++) {
JSONObject mJsonObjectStatement = statementArray.getJSONObject(i);
String date= mJsonObjectStatement.getString("date").split(" ")[0];
if (transactionReportMap.containsKey(date)) {
map.get(date).add(date);
} else {
ArrayList<String> infoList = new ArrayList<String>();
infoList.add(mJsonObjectStatement.getString("Country"));
infoList.add(mJsonObjectStatement.getString("Profession"));
infoList.add(mJsonObjectStatement.getString("Salary"));
map.put(date, infoList);
}
}
StringWriter result = new StringWriter();
try {
mapper.writeValue(result, map);
} catch (IOException e) {
e.printStackTrace();
}
Log.e("Result: ",result+"");
TA贡献1836条经验 获得超13个赞
对于这个问题来说可能为时已晚,但我正在分享我的答案。
//Define an empty hash map
Map<String, JSONObject> finalMap = map;
for (JSONObject object : Statement) {
String key = object.getString("date");
//Split the date from date time
if (key.contains(" ")) {
key = key.split(" ")[0];
}
if (finalMap.containsKey(key)) {
JSONArray list = finalMap.get(key);
list.put(student);
} else {
JSONArray list = new JSONArray();
list.put(object);
finalMap.put(key, list);
}
}
最后,您可以将哈希映射转换为您需要的任何类型。
添加回答
举报