Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.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中解析HTTP JSONObject响应_Java_Http_Post_Jsonobject - Fatal编程技术网

在java中解析HTTP JSONObject响应

在java中解析HTTP JSONObject响应,java,http,post,jsonobject,Java,Http,Post,Jsonobject,从端点“test”返回一个JSONObject: @POST("/test") @PermitAll public JSONObject test(String name) { JSONObject jsonval=new JSONObject(); json.put("key1",true); json.put("key2","test"); return json; } 在检查返回值的方法中,我要搜索“key1”的值。 Buf

从端点“
test
”返回一个
JSONObject

@POST("/test")
    @PermitAll
    public JSONObject test(String name) {
    JSONObject jsonval=new JSONObject();
    json.put("key1",true);
    json.put("key2","test");
        return json;
    }
在检查返回值的方法中,我要搜索“key1”的值。

BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
    String json = null;
    String res = "";

    while ((res = in.readLine()) != null) {
        json += res + "\n";
        }
    in.close();

    if (jsonData has key1 with value true){
    //do sth
    }
    else{
    //do sth else
    }

如何解析返回的JSONObject?

您是否尝试过从其字符串表示形式构造JSONObject(请参阅):

JSONObject结果=新的JSONObject(json)

其中
json
是从
InputStream


注意:您可能需要去掉最后一行字符,甚至完全省略新行

您用于获取类JSONObject的jar是什么?json-simple-1.1.1.jar