我想问我有一些json响应以及为什么我无法从jsonArray中获取值related_news
{
"status": "Success",
"message": "OK",
"query": {
"related": "true",
"is_admin": true
},
"data": {
"title": "Jadwal Pertandingan dan Siaran Langsung Grup D Piala Presiden 2018, Senin 29 Januari",
"news_type": "Liga",
"sub_news_type": "Liga Indonesia",
"credit": "",
"meta_description": "Jadwal Pertandingan dan Siaran Langsung Grup D Piala Presiden 2018, Senin 29 Januari",
"recommended": "1",
"tag": "Piala Presiden",
"slug": "jadwal-pertandingan-dan-siaran-langsung-grup-d-piala-presiden-2018-senin-29-januari",
"createon": "28 January 2018 20:09:31",
"updateon": null,
"publish_on": "2018-01-28 21:50:43",
"news_like": 15,
"news_smile": 0,
"news_shock": 0,
"news_inspired": 0,
"news_happy": 0,
"news_sad": 0,
"news_fear": 0,
"news_angry": 0,
"news_fun": 0,
"username": "bayu_m",
"fullname": "Bayu Mahendra",
"related_news": [
{
"title": "Bhayangkara FC Siapkan Taktik Sapu Jagat Lawan PSM",
"slug": "bhayangkara-fc-siapkan-taktik-sapu-jagat-lawan-psm",
"createon": "17 October 2017 14:47:56",
"updateon": "",
"publish_on": "2017-10-17 14:45:37",
"news_view": 73
}
]
}
}
这里我的执行
JSONObject result = new JSONObject(s);
String status = result.get("status").toString(),
message=result.getString("message");
// Log.d(" log","结果:" + s);
if(status.equals("Success")&& message.equals("OK")){
Toast.makeText(ESNewsDetail.this,"wellcome"+result.getJSONObject("data").getString("fullname"),Toast.LENGTH_LONG).show();
JSONObject data=result.getJSONObject("data");
JSONArray arr=data.getJSONArray("related_news");
String values=arr.getJSONObject(0).getString("id");
for(int chcek=0;chcek<data.length();chcek++){
Toast.makeText(ESNewsDetail.this,"wellcome"+values,Toast.LENGTH_LONG).show();
}
// Toast.makeText(ESNewsDetail.this,"cxcxcx"+String.valueOf(data.getJSONArray("related_news").length()<0?"hmmm":"xxxxx"),Toast.LENGTH_LONG).show();
在这里错了 在related_news中没有价值可以有人帮助我谢谢
答案 0 :(得分:1)
使用result.getString("status")
获取状态,因为状态是一个字符串(&#34;成功&#34;)
JSONObject result = new JSONObject(s);
String status = result.getString("status");
String message=result.getString("message");
检查此代码。
JSONObject data=result.getJSONObject("data");
JSONArray arr=data.getJSONArray("related_news");
for(int chcek=0;chcek<data.length();chcek++){
Toast.makeText(ESNewsDetail.this,"wellcome"+values,Toast.LENGTH_LONG).show();
}
data
不是array
使用此
JSONArray arr=data.getJSONArray("related_news");
for(int chcek=0;chcek<arr.length();chcek++){
JSONObject object = array.getJSONObject(check);
String title = object.getString("title");
Toast.makeText(ESNewsDetail.this,title,Toast.LENGTH_LONG).show();
}
答案 1 :(得分:1)
删除此行 - :
String values=arr.getJSONObject(0).getString("id");
因为id不存在于相关的新闻数组中。请尝试存在的值。
例如 - :
String values=arr.getJSONObject(0).getString("title");
执行将失败并且不会进一步移动,因此请删除该行。
答案 2 :(得分:1)
删除此
arr.getJSONObject(0).getString("id");
id
的密钥在相关的新闻对象中不存在,下一行是一个循环,所以除非第一个元素是特殊的,否则没有必要拥有getJSONObject(0)
,或者你保证有它
事实上,您提供的任何数据中都没有id
。
请仔细检查您的钥匙。
答案 3 :(得分:1)
试试这个
JSONObject result = new JSONObject(s);
String status = result.getString("status");
String message = result.getString("message");
Log.i("log", " result : " + result);
Log.i("Status ", ":" + status);
Log.i("Message ", ":" + message);
if (status.equals("Success") && message.equals("OK")) {
Toast.makeText(MainActivity.this, "wellcome" + result.getJSONObject("data").getString("fullname"), Toast.LENGTH_LONG).show();
JSONObject data = result.getJSONObject("data");
String title = data.getString("title");
String news_type = data.getString("news_type");
String sub_news_type = data.getString("sub_news_type");
String credit = data.getString("credit");
String username = data.getString("username");
String fullname = data.getString("fullname");
Log.i("Data Title ", ":" + title);
Log.i("Data news_type ", ":" + news_type);
Log.i("Data sub_news_type ", ":" + sub_news_type);
Log.i("Data credit ", ":" + credit);
Log.i("Data username ", ":" + username);
Log.i("Data fullname ", ":" + fullname);
JSONArray arr = data.getJSONArray("related_news");
for (int chcek = 0; chcek < arr.length(); chcek++) {
JSONObject jsonObject = arr.getJSONObject(chcek);
String news_title = jsonObject.getString("title");
String slug = jsonObject.getString("slug");
String createon = jsonObject.getString("createon");
String updateon = jsonObject.getString("updateon");
String publish_on = jsonObject.getString("publish_on");
String news_view = jsonObject.getString("news_view");
Log.i("News Title ", ":" + news_title);
Log.i("News slug ", ":" + slug);
Log.i("News createon ", ":" + createon);
Log.i("News updateon ", ":" + updateon);
Log.i("News publish_on ", ":" + publish_on);
Log.i("News news_view ", ":" + news_view);
//Toast.makeText(MainActivity.this, title + "This is title value", Toast.LENGTH_LONG).show();
}
}
<强>输出强>
答案 4 :(得分:0)
在代码中:
JSONArray arr=data.getJSONArray("related_news");
String values=arr.getJSONObject(0).getString("id");
有关键&#34; id&#34;。但字符串json没有密钥&#34; id&#34;。请将ID更改为标题以进行检查。
代码示例
try {
JSONObject result = new JSONObject(getResponse());
String status = result.getString("status");
String message = result.getString("message");
if (!TextUtils.isEmpty(status) && !TextUtils.isEmpty(message)) {
JSONObject data = result.getJSONObject("data");
JSONArray arr = data.getJSONArray("related_news");
for (int chcek = 0; chcek < arr.length(); chcek++) {
String values = arr.getJSONObject(chcek).getString("title");
Log.e("TAG", "welcome: " + values);
}
}
}catch (Exception ex){
Log.e("TAG", "error: " + ex.toString());
}
答案 5 :(得分:0)
请使用has()方法,如果key存在则返回true,否则为
if (status.equals("Success") && message.equals("OK")) {
Toast.makeText(ESNewsDetail.this, "wellcome" + result.getJSONObject("data").getString("fullname"), Toast.LENGTH_LONG).show();
JSONObject data = result.getJSONObject("data");
JSONArray arr = null;
try {
arr = data.getJSONArray("related_news");
String values = arr.getJSONObject(0).has("id") ? arr.getJSONObject(0).getString("id") : "Key not found";
Toast.makeText(ESNewsDetail.this, "wellcome" + values, Toast.LENGTH_LONG).show();
} catch (JSONException e) {
e.printStackTrace();
}
}