如何从 Android 中的 post 请求响应中获取特定的 json 键值?
How to get a specific json key value from post request response in Android?
我收到来自 post 使用 HttpURLConnection
请求的以下响应:
Post 请求响应:
{
"LatestData": [{
"ExtraData": null,
"ID": 0,
"season": false,
"latest": 0,
"url": "http://www.awebsite.com/images/12.jpg"
}]
}
如何获取URL的值?我试过以下但 Android Studio 一直给我错误:
String newURL = sb.getJSONObject("LatestData").getString("url");
String newURL = sb.getJSONArray("LatestData").getJSONObject(0).getString("url");
Android 工作室错误:
error: cannot find symbol method getJSONObject(String)
error: cannot find symbol method getJSONArray(String)
你们能帮我获取 url 的值并让我知道我需要将哪些库导入到 android studio 以便 getsonObject 工作吗?谢谢
android代码:
if (myURLConnection.getResponseCode() == 200) {
br = new BufferedReader(new InputStreamReader(myURLConnection.getInputStream(), "utf-8"));
while (true) {
line = br.readLine();
if (line != null) {
sb.append(line + "\n");
//String newURL = sb.getJSONObject("LatestData").getString("url");
String newURL =sb.getJSONArray("LatestData").getJSONObject(0).getString("url");
mWebView.loadUrl("javascript:MyFunction('" +newURL + "');");
} else {
br.close();
return sb.toString();
}
}
}
try {
JSONObject jsonObject = new JSONObject(sb);
String url = jsonObject.optJSONArray("LatestData").getJSONObject(0).optString("url");
} catch (JSONException e) {
e.printStackTrace();
}
您需要将 sb 转换为 JSONObject 才能访问属性:
JSONOjbect jsonResponse = new JSONObject(new String(sb));
然后:
try {
JSONArray jsonArray = jsonResponse.getJSONArray("LatestData");
if (jsonArray != null && jsonArray.length() > 0) {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject dataOjbect = jsonArray.getJSONObject(i);
String url = dataOjbect.getString("url");
}
}
} catch (Exception e) {
e.printStackTrace();
}
你的方法应该是这样的。
try {
List<String> urlist = new ArrayList<>();
JSONObject jsonObject = new JSONObject(sb.toString());
JSONArray jsonArray = jsonObject.getJSONArray("LatestData");
for (int count = 0; count<jsonArray.length(); count++){
JSONObject jsonInner = jsonArray.getJSONObject(count);
String url = jsonInner.getString("url");
//store your url in some list
urlist.add(url);
}
} catch (JSONException e) {
e.printStackTrace();
}
尝试使用 loadJSONArray
String url = loadJSONArray(sb)..getJSONObject(0).getString("url");
你能试试这个吗:
StringBuilder sb = new StringBuilder();
line = br.readLine();
while(line != null){
sb.append(line);
}
// if it's fail here then this means there is an issue in parsing JSONObject
JSONObject jsonObj = new JSONObject(sb.toString());
JSONArray latestData = jsonObj.getJSONArray("LatestData");
JSONObject jsonObject = latestData.getJSONObject(0);
String url = jsonObject.getString("url");
此外,我强烈建议您将 Retrofit 与 GSON 结合使用。您可以阅读这篇文章:http://www.vogella.com/tutorials/Retrofit/article.html
使用 jsonschema2pojo 为 JSON 数据生成 pojo 类
我收到来自 post 使用 HttpURLConnection
请求的以下响应:
Post 请求响应:
{
"LatestData": [{
"ExtraData": null,
"ID": 0,
"season": false,
"latest": 0,
"url": "http://www.awebsite.com/images/12.jpg"
}]
}
如何获取URL的值?我试过以下但 Android Studio 一直给我错误:
String newURL = sb.getJSONObject("LatestData").getString("url");
String newURL = sb.getJSONArray("LatestData").getJSONObject(0).getString("url");
Android 工作室错误:
error: cannot find symbol method getJSONObject(String)
error: cannot find symbol method getJSONArray(String)
你们能帮我获取 url 的值并让我知道我需要将哪些库导入到 android studio 以便 getsonObject 工作吗?谢谢
android代码:
if (myURLConnection.getResponseCode() == 200) {
br = new BufferedReader(new InputStreamReader(myURLConnection.getInputStream(), "utf-8"));
while (true) {
line = br.readLine();
if (line != null) {
sb.append(line + "\n");
//String newURL = sb.getJSONObject("LatestData").getString("url");
String newURL =sb.getJSONArray("LatestData").getJSONObject(0).getString("url");
mWebView.loadUrl("javascript:MyFunction('" +newURL + "');");
} else {
br.close();
return sb.toString();
}
}
}
try {
JSONObject jsonObject = new JSONObject(sb);
String url = jsonObject.optJSONArray("LatestData").getJSONObject(0).optString("url");
} catch (JSONException e) {
e.printStackTrace();
}
您需要将 sb 转换为 JSONObject 才能访问属性:
JSONOjbect jsonResponse = new JSONObject(new String(sb));
然后:
try {
JSONArray jsonArray = jsonResponse.getJSONArray("LatestData");
if (jsonArray != null && jsonArray.length() > 0) {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject dataOjbect = jsonArray.getJSONObject(i);
String url = dataOjbect.getString("url");
}
}
} catch (Exception e) {
e.printStackTrace();
}
你的方法应该是这样的。
try {
List<String> urlist = new ArrayList<>();
JSONObject jsonObject = new JSONObject(sb.toString());
JSONArray jsonArray = jsonObject.getJSONArray("LatestData");
for (int count = 0; count<jsonArray.length(); count++){
JSONObject jsonInner = jsonArray.getJSONObject(count);
String url = jsonInner.getString("url");
//store your url in some list
urlist.add(url);
}
} catch (JSONException e) {
e.printStackTrace();
}
尝试使用 loadJSONArray
String url = loadJSONArray(sb)..getJSONObject(0).getString("url");
你能试试这个吗:
StringBuilder sb = new StringBuilder();
line = br.readLine();
while(line != null){
sb.append(line);
}
// if it's fail here then this means there is an issue in parsing JSONObject
JSONObject jsonObj = new JSONObject(sb.toString());
JSONArray latestData = jsonObj.getJSONArray("LatestData");
JSONObject jsonObject = latestData.getJSONObject(0);
String url = jsonObject.getString("url");
此外,我强烈建议您将 Retrofit 与 GSON 结合使用。您可以阅读这篇文章:http://www.vogella.com/tutorials/Retrofit/article.html
使用 jsonschema2pojo 为 JSON 数据生成 pojo 类