gpt4 book ai didi

java - JSONException : Misplaced object

转载 作者:太空宇宙 更新时间:2023-11-04 08:46:36 25 4
gpt4 key购买 nike

这是我的代码:

    JSONStringer result = new JSONStringer();

for (long i = start; i <= end; i = i + day) {
ttm.put("$gte", "" + i);
ttm.put("$lte", "" + (i + day));
//code code code

int count = statisticCollection.find(query).count();

try {
result.object().key("ttm").value(i).key("count").value(count);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

try {
result.endObject();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

然后我得到一个 JSONException。我还尝试使用不同的 try-catch block 创建和结束对象,如下所示:

    JSONStringer result = new JSONStringer();

try {
result.object();
} catch (Exception e) {
e.printStackTrace();
}

for (long i = start; i <= end; i = i + day) {
ttm.put("$gte", "" + i);
ttm.put("$lte", "" + (i + day));

//code code code

long count = statisticCollection.find(query).count();

try {
result.key("ttm").value(i).key("count").value(count);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

try {
result.endObject();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

并在 for 循环本身中创建和结束 JSONStringer,如下所示:

JSONStringer result = new JSONStringer();

for (long i = start; i <= end; i = i + day) {
ttm.put("$gte", "" + i);
ttm.put("$lte", "" + (i + day));
//code code code

int count = statisticCollection.find(query).count();

try {
result.object().key("ttm").value(i).key("count").value(count).endObject();
} catch (JSONException e) {
e.printStackTrace();
}

我做错了什么?

谢谢。

最佳答案

您需要使用数组:

JSONStringer result = new JSONStringer();
JSONWriter array = result.array();

for (long i = start; i <= end; i = i + day) {
ttm.put("$gte", "" + i);
ttm.put("$lte", "" + (i + day));
//code code code

int count = statisticCollection.find(query).count();

try {
array.object().key("ttm").value(i).key("count").value(count).endObject();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

try {
array.endArray();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

关于java - JSONException : Misplaced object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4183009/

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