从 Java 中的 HTTP 响应解析 JSON
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35530849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Parsing a JSON from HTTP Response in Java
提问by VipPunkJoshers Droopy
Hi I am using Client Http (apache), and json-simple.
嗨,我正在使用客户端 Http (apache) 和 json-simple。
I want to access the attributes of the json response, and then use them.
我想访问json响应的属性,然后使用它们。
Any idea how to do this? I read a post and did not work as it but me.
知道如何做到这一点吗?我读了一篇文章,但我并没有像它那样工作。
This is my answer json:
这是我的回答 json:
{"Name":"myname","Lastname":"mylastname","Age":19}
This is my code java:
这是我的代码java:
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet getRequest = new HttpGet(
"http://localhost:8000/responsejava");
getRequest.addHeader("accept", "application/json");
HttpResponse response = httpClient.execute(getRequest);
if (response.getStatusLine().getStatusCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ response.getStatusLine().getStatusCode());
}
BufferedReader br = new BufferedReader(
new InputStreamReader(
(response.getEntity().getContent())
)
);
StringBuilder content = new StringBuilder();
String line;
while (null != (line = br.readLine())) {
content.append(line);
}
Object obj=JSONValue.parse(content.toString());
JSONObject finalResult=(JSONObject)obj;
System.out.println(finalResult);
httpClient.getConnectionManager().shutdown();
I printed null, What am I doing wrong?
我打印了 null,我做错了什么?
采纳答案by armysheng
Better and easier to use Gson
更好更易用的Gson
Gson gson = new Gson;
NameBean name = gson.fromJson(content.toString(),NameBean.class)
NameBean
is the object where you persist the json string.
NameBean
是您保存 json 字符串的对象。
public class NameBean implements Serializable{
public String name;
public String lastname;
public Int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public Int getAge() {
return age;
}
public void setAge(Int age) {
this.age = age;
}
}
}
回答by David Sousa
instead of
代替
Object obj=JSONValue.parse(content.toString());
JSONObject finalResult=(JSONObject)obj;
System.out.println(finalResult);
try this:
尝试这个:
JSONObject jsonObject = new JSONObject(content.toString());
System.out.println(jsonObject.getString("Name") + " " jsonObject.getString("Lastname") + " " + jsonObject.getInt("Age"));
回答by Beno Arakelyan
I higly recomend http-requestbuilt on apache http api.
我强烈推荐建立在 apache http api 上的http-request。
HttpRequest<Data> httpRequest = HttpRequestBuilder.createGet(yourUri, Data.class)
.addDefaultHeader("accept", "application/json")
.build();
public void send(){
ResponseHandler<Data> responseHandler = httpRequest.execute();
Data data = responseHandler.orElseThrow(); // returns the data or throws ResponseException If response code is not success
}
Data
class which you get as response.
Data
你得到的响应的类。
public Data{
private String Name;
private String Lastname;
private int Age;
// getters and setters
}
I also recommend watching my answer hereIf you want get response as String
我还建议您在此处观看我的回答如果您想以字符串形式获得响应