我想从Http get响应中获取一个JSON
对象:
这是我当前的Http get代码:
protected String doInBackground(String... params) {
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(params[0]);
HttpResponse response;
String result = null;
try {
response = client.execute(request);
HttpEntity entity = response.getEntity();
if (entity != null) {
// A Simple JSON Response Read
InputStream instream = entity.getContent();
result = convertStreamToString(instream);
// now you have the string representation of the HTML request
System.out.println("RESPONSE: " + result);
instream.close();
if (response.getStatusLine().getStatusCode() == 200) {
netState.setLogginDone(true);
}
}
// Headers
org.apache.http.Header[] headers = response.getAllHeaders();
for (int i = 0; i < headers.length; i++) {
System.out.println(headers[i]);
}
} catch (ClientProtocolException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return result;
}
这是转换的SteamToString函数:
private static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
现在我只是得到一个字符串对象。如何取回JSON对象。
这不是你问题的确切答案,但这可能会对你有所帮助
public class JsonParser {
private static DefaultHttpClient httpClient = ConnectionManager.getClient();
public static List<Club> getNearestClubs(double lat, double lon) {
// YOUR URL GOES HERE
String getUrl = Constants.BASE_URL + String.format("getClosestClubs?lat=%f&lon=%f", lat, lon);
List<Club> ret = new ArrayList<Club>();
HttpResponse response = null;
HttpGet getMethod = new HttpGet(getUrl);
try {
response = httpClient.execute(getMethod);
// CONVERT RESPONSE TO STRING
String result = EntityUtils.toString(response.getEntity());
// CONVERT RESPONSE STRING TO JSON ARRAY
JSONArray ja = new JSONArray(result);
// ITERATE THROUGH AND RETRIEVE CLUB FIELDS
int n = ja.length();
for (int i = 0; i < n; i++) {
// GET INDIVIDUAL JSON OBJECT FROM JSON ARRAY
JSONObject jo = ja.getJSONObject(i);
// RETRIEVE EACH JSON OBJECT'S FIELDS
long id = jo.getLong("id");
String name = jo.getString("name");
String address = jo.getString("address");
String country = jo.getString("country");
String zip = jo.getString("zip");
double clat = jo.getDouble("lat");
double clon = jo.getDouble("lon");
String url = jo.getString("url");
String number = jo.getString("number");
// CONVERT DATA FIELDS TO CLUB OBJECT
Club c = new Club(id, name, address, country, zip, clat, clon, url, number);
ret.add(c);
}
} catch (Exception e) {
e.printStackTrace();
}
// RETURN LIST OF CLUBS
return ret;
}
}
Again, it’s relatively straight forward, but the methods I’ll make special note of are:
JSONArray ja = new JSONArray(result);
JSONObject jo = ja.getJSONObject(i);
long id = jo.getLong("id");
String name = jo.getString("name");
double clat = jo.getDouble("lat");
得到的字符串就是JSON对象。toString()。这意味着您获得了JSON对象,但采用了字符串格式。
如果你想得到一个JSON对象,你可以放:
JSONObject myObject = new JSONObject(result);
问题内容: 我想从Http get响应中获取一个对象: 这是我当前对Http get的代码: 这是convertSteamToString函数: 现在,我只是得到一个字符串对象。我如何找回JSON对象。 问题答案: 您获得的字符串只是JSON Object.toString()。这意味着您将获取JSON对象,但使用String格式。 如果应该获取JSON对象,则可以输入:
我试图从Web读取JSON数据,但该代码返回空结果。我不确定我做错了什么。
问题内容: 我是Node开发的新手,我正在尝试使用具有JSON响应的RESTful协议进行服务器端API调用。我已经阅读了API文档和本SO帖子。 我试图从轨道总线中提取的API并以JSON输出返回数据。我对如何使用实际URL中的所有参数和选项发出HTTP GET请求感到困惑。甚至可以通过浏览器或使用“ curl”命令来访问API及其响应。http://developer.cumtd.com/ap
我正在使用Spring Web创建一个rest资源。我用Java创建了类似于Json响应的文档对象。当我使用String.class作为响应类型时,我从请求中获取响应。当我更改对我创建的类的响应时,boy是null。 这是我的httpgetter Json文档 函数调用 JSON响应
问题内容: 我正在尝试从Web读取JSON数据,但是该代码返回空结果。我不确定我在做什么错。 问题答案: 理想的方法 不是 使用,而是直接在阅读器上使用解码器。这是一个不错的函数,它获取url并将其响应解码到结构上。 使用示例: 您不应该在生产中使用默认结构,如最初回答的那样!(/ etc调用的是哪个)。原因是默认客户端没有设置超时。如果远程服务器无响应,那将是糟糕的一天。
我已经使用API从API中提取了JSON响应,请放心,它看起来像这样: 现在,我实际的JSON响应在JSON数组中有数千个JSON对象,一些键有空值,例如“secondKey”在一些JSON对象中有空值。我需要获取JSON响应中所有空值的键。对我该如何做有什么想法吗? 我解决这个问题的想法是使用Jackson库反序列化JSON并获取所有空值。然而,考虑到性能,是否有任何有效的解决方案?