Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将JSONArray转换为JSONObject_Java_Json_Rest - Fatal编程技术网

Java 将JSONArray转换为JSONObject

Java 将JSONArray转换为JSONObject,java,json,rest,Java,Json,Rest,我正在编写一个RESTJava服务。我想将我的JSONArray转换为JSONObject并返回它。但当我从浏览器点击rest服务时,我得到了“{}”作为输出。尽管当我尝试使用System.out.println()打印时,rest服务内部的打印效果很好 PreparedStatement dimDelPS=null; 结果集dimDelRS=null; dimDelPS=连接.prepareStatement(“从abc中选择*); dimDelRS=dimDelPS.executeQuery

我正在编写一个RESTJava服务。我想将我的JSONArray转换为JSONObject并返回它。但当我从浏览器点击rest服务时,我得到了“{}”作为输出。尽管当我尝试使用System.out.println()打印时,rest服务内部的打印效果很好

PreparedStatement dimDelPS=null;
结果集dimDelRS=null;
dimDelPS=连接.prepareStatement(“从abc中选择*);
dimDelRS=dimDelPS.executeQuery();
字符串dimLow=null;
while(dimdels.next()){
int total_rows=dimdels.getMetaData().getColumnCount();
对于(int i=0;i
您不能只返回JSONObject

您需要确保将其打包成json

'返回Response.ok(jsonObject.toString(),MediaType.APPLICATION_JSON).build();'


浏览器理解字符串,而不是java对象。

不,它不工作,浏览器显示{}您没有提供足够的信息。就我们所知,上面的代码片段与浏览器或REST调用无关。您使用的是框架吗?整个方法看起来像什么?请再回顾一下,谁对这个问题投了赞成票?这太糟糕了。我认为问题在于json。你的json应该像{“aoColumnDefs”:[{“employee”:“ANTHONY.DUNNE”},{“type”:“Manager”}}请容忍我的错误格式,从手机上回答。这使得一个相当大的假设OP使用JAX-RS。问题中没有证据支持这一点。它不起作用。我只是将返回类型替换为Response,最后一行替换为您提供的内容。仍然是浏览器上的{}。谢谢,伙计,这起作用了。返回Response.ok(jsobobject.toString(),MediaType.APPLICATION_JSON).build();太好了,你能更新/编辑答案和格式吗
PreparedStatement dimDelPS = null;
ResultSet dimDelRS = null;
dimDelPS = connection.prepareStatement("select * from abc");
dimDelRS = dimDelPS.executeQuery();
String dimLow=null;

while (dimDelRS.next()) {
    int total_rows = dimDelRS.getMetaData().getColumnCount();
    for (int i = 0; i < total_rows; i++) {
        org.json.JSONObject obj = new org.json.JSONObject();
        obj.put(dimDelRS.getMetaData().getColumnLabel(i + 1)
                .toLowerCase(), dimDelRS.getObject(i + 1));
        jsonArray.put(obj);
    }
}

System.out.println("json1 :"+jsonArray);

//Sample output at this stage: ["{\"employee\":\"ANTHONY.DUNNE\"}","{\"type\":\"Manager\"}"]

dimDelRS.close();
dimDelPS.close();
JSONObject jsobobject= new JSONObject();
jsobobject.put("aoColumnDefs",jsonArray);
System.out.println(jsobobject);
return jsobobject;