我从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
答案 0 :(得分:3)
根据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_msg
,short_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);
到
if (json.has("alert")) {
JSONObject alert = json.getJSONObject("alert");
String _title = alert.getString(Constansts.PARSE.NOTIFICATION.TITLE);
String _shortMsg = alert.getString(Constansts.PARSE.NOTIFICATION.SHORT_MSG);
String _longMsg = alert.getString(Constansts.PARSE.NOTIFICATION.LONG_MSG);
答案 1 :(得分:0)
首先在警报下打开JSON,然后在该引用下打包其他字符串值
JSONObject alert=json.getJSONObject("alert");;
String _title = alert.getString(Constansts.PARSE.NOTIFICATION.TITLE);
答案 2 :(得分:0)
如果你想要解决这些问题,那么你必须检查响应中的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);
}
}
它永远不会抛出异常....你现在得到了什么