例如,我得到如下所示的 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)
.
}
上面的序列按照一定的时间间隔重复更新,比如 1 小时更新一个新的 json。 我要做的就是整理出当天的序列,并总结条目日志中的命中和片段,最后将它们放入字符串数组中,如下所示:
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"
}
]
}
然后你可以将“log”的整个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/