java - JSONException : Misplaced object

标签 java gwt json

这是我的代码:

    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/

相关文章:

java - Maven GWT编译器编译两次?

javascript - 如何限制我的node.js Swagger 服务器的堆栈跟踪?

java - 声明多个类变量 Java

java : return inherited child class type

gwt - 如何调整 GWT UIBinder 模板中 SplitLayoutPanel 拖动器的大小

java - 在 ant 脚本中运行 GWTComplier 时出现问题

c - gSOAP:如何在 C 中比较两个值结构(带有 JSON 内容)?

java - 忽略嵌套响应类的根 json 键

java - 如何在 Struts 2 中解析请求 URL?

java - Spring 安全 : How to reject a request by default if no @PreAuthorize was specified