Android,org.json.JSONException:No value for

Android,org.json.JSONException:No value for,android,json,Android,Json,我从webservice获得以下json "alert": { "long_msg": "Thank you for downloading PHARMAPLUS APP, if you found our APP helpful please rate us five star on play store", "short_msg": "Welcome to PHARMAPLUS", "title": "Thank you" } 我的代码是 if (js

我从webservice获得以下json

"alert": { 
    "long_msg": "Thank you for downloading PHARMAPLUS APP, if you found our APP helpful please rate us five star on play store", 
    "short_msg": "Welcome to PHARMAPLUS", 
    "title": "Thank you" 
 } 
我的代码是

 if (json.has("alert")) {
     String alert = json.getString("alert");
     String _title = json.getString(Constansts.PARSE.NOTIFICATION.TITLE);
     String _shortMsg = json.getString(Constansts.PARSE.NOTIFICATION.SHORT_MSG);
     String _longMsg = json.getString(Constansts.PARSE.NOTIFICATION.LONG_MSG);
常数的定义如下所示

 public static final String TITLE = "title";
 public static final String SHORT_MSG = "short_msg";
 public static final String LONG_MSG = "long_msg";
org.json.JSONException: No value for title
得到如下错误

 public static final String TITLE = "title";
 public static final String SHORT_MSG = "short_msg";
 public static final String LONG_MSG = "long_msg";
org.json.JSONException: No value for title

根据
JSON

"alert": { 
   "long_msg": "Thank you for downloading PHARMAPLUS APP, if you found our APP helpful please rate us five star on play store", 
   "short_msg": "Welcome to PHARMAPLUS", 
   "title": "Thank you" 
 }
这里的警报是一个
JSONObject
。值
title
long\u msg
short\u msg
alert

因此,您需要修改这部分代码

if (json.has("alert")) {
     String alert = json.getString("alert");
     String _title = json.getString(Constansts.PARSE.NOTIFICATION.TITLE);
     String _shortMsg = json.getString(Constansts.PARSE.NOTIFICATION.SHORT_MSG);
     String _longMsg = json.getString(Constansts.PARSE.NOTIFICATION.LONG_MSG);
进入


根据
JSON

"alert": { 
   "long_msg": "Thank you for downloading PHARMAPLUS APP, if you found our APP helpful please rate us five star on play store", 
   "short_msg": "Welcome to PHARMAPLUS", 
   "title": "Thank you" 
 }
这里的警报是一个
JSONObject
。值
title
long\u msg
short\u msg
alert

因此,您需要修改这部分代码

if (json.has("alert")) {
     String alert = json.getString("alert");
     String _title = json.getString(Constansts.PARSE.NOTIFICATION.TITLE);
     String _shortMsg = json.getString(Constansts.PARSE.NOTIFICATION.SHORT_MSG);
     String _longMsg = json.getString(Constansts.PARSE.NOTIFICATION.LONG_MSG);
进入


首先在alert下输入JSON,然后在该引用下输入其他字符串值

JSONObject alert=json.getJSONObject("alert");;
String _title = alert.getString(Constansts.PARSE.NOTIFICATION.TITLE);

首先在alert下输入JSON,然后在该引用下输入其他字符串值

JSONObject alert=json.getJSONObject("alert");;
String _title = alert.getString(Constansts.PARSE.NOTIFICATION.TITLE);

如果您想解决这些问题,那么您必须检查json或字符串是否在响应中退出,而不仅仅是

JSONObject jsonObject = new JSONObject(respons);///Here response is your response

if(jsonObject.has("alert"))
{
 JSONObject alert = json.getJSONObject("alert");

 if(alert.has("long_msg"))
 {
  String _longMsg = alert.getString(Constansts.PARSE.NOTIFICATION.LONG_MSG);
 }

if(alert.has("short_msg"))
 {
  String _shortMsg = alert.getString(Constansts.PARSE.NOTIFICATION.SHORT_MSG);
 }

if(alert.has("title"))
 {
  String _title = alert.getString(Constansts.PARSE.NOTIFICATION.TITLE);
 }
}

它永远不会抛出异常……如果你想解决这些问题,你现在得到的是什么

JSONObject jsonObject = new JSONObject(respons);///Here response is your response

if(jsonObject.has("alert"))
{
 JSONObject alert = json.getJSONObject("alert");

 if(alert.has("long_msg"))
 {
  String _longMsg = alert.getString(Constansts.PARSE.NOTIFICATION.LONG_MSG);
 }

if(alert.has("short_msg"))
 {
  String _shortMsg = alert.getString(Constansts.PARSE.NOTIFICATION.SHORT_MSG);
 }

if(alert.has("title"))
 {
  String _title = alert.getString(Constansts.PARSE.NOTIFICATION.TITLE);
 }
}

它永远不会抛出异常……您现在得到的是什么?JSON没有“title”的值。“alert”键处的对象没有。为什么
stringalert=json.getString(“alert”)?JSON没有“title”的值。“警报”键处的对象不存在。