在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