gpt4 book ai didi

java - 如何将多次重复的json数组排序为不重复的字符串数组?

转载 作者:行者123 更新时间:2023-11-30 07:01:02 24 4
gpt4 key购买 nike

例如,我得到一个像下面这样的json链:

 {
"error": false,
"entry_log": [651],
{
"id": 2073,
"hit": 2,
"amount": 3,
"pieces": 2,
"user_email": "xxxxx",
"log_time": "2015-05-07 13:05:21"
},
{
"id": 2069,
"hit": 1,
"amount": 3,
"pieces": 4,
"user_email": "xxxxx",
"log_time": "2015-05-07 12:05:25"
},
{
"id": 2065,
"hit": 0,
"amount": 0,
"pieces": 0,
"user_email": "xxxxx",
"log_time": "2015-05-06 09:05:38"
},


.
. (repeated sequence with the same or different log_time)
.
}

以上序列按固定时间间隔重复更新,比如一个新json 1 小时。我要做的是整理出同一天的序列,并从 entry_log 中总结命中和片段,最后将它们放入一个字符串数组中,例如成为:

array1[0] = "2015-05-07"   array1[1] = "2015-05-06" //log_time
array2[0] = "3" array2[1] = "0" //hit
array3[0] = "6" array3[1] = "0" // pieces
....and so on

因此,我有以下代码来整理它们:

JSONObject reader = new JSONObject(json); // this is the json of the above sequence
JSONArray entry_log = reader.getJSONArray("entry_log");
JSONObject inside_entry_log;

String[] date = new String[entry_log.length()];
String[] hit = new String[entry_log.length()];// I ignore this at this moment
String[] pieces = new String[entry_log.length()];
// I am quite unfamiliar with the array object, probably i should use arraylist here
int array_row = 0;
for (int i = 0; i < entry_log.length(); i++) {
inside_entry_log = entry_log.getJSONObject(i);
hit = inside_entry_log.getString("hit");
String new_pieces = inside_entry_log.getString("pieces");
String new_date = inside_entry_log.getString("log_time");
String[] Split2 = new_date.split("\\s+");
if (i == 0) {
date[0] = Split2[0];
pieces[0] = new_pieces;
}
if (i != 0) {
if (date[array_row].matches(Split2[0])) {
pieces[array_row] =
Float.toString(Float.parseFloat(pieces[array_row]) + Float.parseFloat(new_pieces));
} else {
array_row += 1;
date[array_row] = Split2[0];
pieces[array_row] = new_pieces;
}
}
}

但是一旦序列越来越多,运行速度就会变慢。那么它是对它们进行排序的唯一方法还是有什么聪明的方法可以加快排序速度?非常感谢您回答我的问题!!

最佳答案

@Dexter .. 首先我发现你的 json 有点典型.. 正如你所说的

 "entry_log": [
651
]

而 entry_log 只包含单个 int.. 所以为什么 jsonarray

你可以先这样格式化你的json

{
"error": false,
"entry_log": 651,
"log": [
{
"id": 2073,
"hit": 2,
"amount": 3,
"pieces": 2,
"user_email": "xxxxx",
"log_time": "2015-05-07 13:05:21"
},
{
"id": 2069,
"hit": 1,
"amount": 3,
"pieces": 4,
"user_email": "xxxxx",
"log_time": "2015-05-07 12:05:25"
},
{
"id": 2065,
"hit": 0,
"amount": 0,
"pieces": 0,
"user_email": "xxxxx",
"log_time": "2015-05-06 09:05:38"
}
]
}

然后你可以将“日志”的整个jsonarray保存到jsonArray对象或者序列化到java对象

为此 json 创建等效对象例如-

public class LogModal {
/**
* entry_log : 651
* log : [{"pieces":2,"hit":2,"amount":3,"user_email":"xxxxx","id":2073,"log_time":"2015-05-07 13:05:21"},{"pieces":4,"hit":1,"amount":3,"user_email":"xxxxx","id":2069,"log_time":"2015-05-07 12:05:25"},{"pieces":0,"hit":0,"amount":0,"user_email":"xxxxx","id":2065,"log_time":"2015-05-06 09:05:38"}]
* error : false
*/
private int entry_log;
private List<LogEntity> log;
private boolean error;

public void setEntry_log(int entry_log) {
this.entry_log = entry_log;
}

public void setLog(List<LogEntity> log) {
this.log = log;
}

public void setError(boolean error) {
this.error = error;
}

public int getEntry_log() {
return entry_log;
}

public List<LogEntity> getLog() {
return log;
}

public boolean isError() {
return error;
}

public class LogEntity {
/**
* pieces : 2
* hit : 2
* amount : 3
* user_email : xxxxx
* id : 2073
* log_time : 2015-05-07 13:05:21
*/
private int pieces;
private int hit;
private int amount;
private String user_email;
private int id;
private String log_time;

public void setPieces(int pieces) {
this.pieces = pieces;
}

public void setHit(int hit) {
this.hit = hit;
}

public void setAmount(int amount) {
this.amount = amount;
}

public void setUser_email(String user_email) {
this.user_email = user_email;
}

public void setId(int id) {
this.id = id;
}

public void setLog_time(String log_time) {
this.log_time = log_time;
}

public int getPieces() {
return pieces;
}

public int getHit() {
return hit;
}

public int getAmount() {
return amount;
}

public String getUser_email() {
return user_email;
}

public int getId() {
return id;
}

public String getLog_time() {
return log_time;
}
}
}

然后使用像 jackson/gson 这样的库来序列化到有助于固定你的应用程序的 java 对象

前-

LogModal logmodal=gson.fromJson(jsonString, LogModal);

关于java - 如何将多次重复的json数组排序为不重复的字符串数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30094286/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com