从解析Java中的HTTP响应JSON数组 [英] Parsing a JSON Array from HTTP Response in Java
问题描述
我使用Apache的HTTP客户端,我试图解析来自我从客户端得到的回应一个JSON阵列。
这是我收到回JSON的例子。
[{\"created_at\":\"2013-04-02T23:07:32Z\",\"id\":1,\"password_digest\":\"$2a$10$kTITRarwKawgabFVDJMJUO/qxNJQD7YawClND.Hp0KjPTLlZfo3oy\",\"updated_at\":\"2013-04-02T23:07:32Z\",\"username\":\"eric\"},{\"created_at\":\"2013-04-03T01:26:51Z\",\"id\":2,\"password_digest\":\"$2a$10$1IE6hR4q5jQrYBtyxMJJBOGwSPQpg6m5.McNDiSIETBq4BC3nUnj2\",\"updated_at\":\"2013-04-03T01:26:51Z\",\"username\":\"Sean\"}]
我使用 HTTP://$c$c.google.com/ p / JSON-简单/ 我的JSON库。
HttpPost httppost =新HttpPost(SERVERURL);
httppost.setEntity(输入);
HTT presponse响应= httpclient.execute(httppost);
RD的BufferedReader =新的BufferedReader(新的InputStreamReader(response.getEntity()的getContent())) 对象物obj = JSONValue.parse(rd.toString());
JSONArray finalResult =(JSONArray),其中OBJ;
的System.out.println(finalResult);
下面是code我都试过,但它不工作。我真的不知道该怎么做。任何帮助是AP preciated,谢谢。
的BufferedReader RD =新的BufferedReader(新的InputStreamReader(response.getEntity()的getContent()))
obj对象= JSONValue.parse(rd.toString());
块引用>
rd.toString()
不会给你的,内容的InputStream
对应response.getEntity()的getContent()
。相反,它给人的的toString()
再一个的BufferedReader
对象presentation。尝试打印它在控制台上,看看它是什么。相反,你应该从读取数据
的BufferedReader
如下:StringBuilder的内容=新的StringBuilder();
串线;
而(空!=(线= br.readLine()){
content.append(线);
}然后,您应该分析具体内容,以获得JSON阵列。
obj对象= JSONValue.parse(content.toString());
JSONArray finalResult =(JSONArray),其中OBJ;
的System.out.println(finalResult);I am using the HTTP client from Apache, and am trying to parse a JSON array from the response I get from the client.
This is an example of the JSON I receive back.
[{"created_at":"2013-04-02T23:07:32Z","id":1,"password_digest":"$2a$10$kTITRarwKawgabFVDJMJUO/qxNJQD7YawClND.Hp0KjPTLlZfo3oy","updated_at":"2013-04-02T23:07:32Z","username":"eric"},{"created_at":"2013-04-03T01:26:51Z","id":2,"password_digest":"$2a$10$1IE6hR4q5jQrYBtyxMJJBOGwSPQpg6m5.McNDiSIETBq4BC3nUnj2","updated_at":"2013-04-03T01:26:51Z","username":"Sean"}]
I am using http://code.google.com/p/json-simple/ as my json library.
HttpPost httppost = new HttpPost("SERVERURL"); httppost.setEntity(input); HttpResponse response = httpclient.execute(httppost); BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())) Object obj=JSONValue.parse(rd.toString()); JSONArray finalResult=(JSONArray)obj; System.out.println(finalResult);
Here is the code I have tried but it doesn't work. I am not really sure what to do. Any help is appreciated, thanks.
解决方案BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())) Object obj=JSONValue.parse(rd.toString());
rd.toString()
would not give you the content of thatInputStream
corresponding toresponse.getEntity().getContent()
. It instead gives thetoString()
representation of aBufferedReader
object. Try printing it on your console to see what it is.Instead you should read the data from the
BufferedReader
as follows:StringBuilder content = new StringBuilder(); String line; while (null != (line = br.readLine()) { content.append(line); }
Then, you should parse the content to get the JSON array.
Object obj=JSONValue.parse(content.toString()); JSONArray finalResult=(JSONArray)obj; System.out.println(finalResult);
这篇关于从解析Java中的HTTP响应JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!