我得到结果json并在android中解析。消息发生了异常:org.json.JSONException:价格没有值
W/System.err: org.json.JSONException: No value for price
W/System.err: at org.json.JSONObject.get(JSONObject.java:389)
W/System.err: at org.json.JSONObject.getString(JSONObject.java:550)
Run Code Online (Sandbox Code Playgroud)
我的java课
JSONParser jsonParser = new JSONParser();
// tracks JSONArray
JSONArray albums = null;
// Album id
String album_id = null;
String song_id = null;
String album_name, song_name, price;
// single song JSON url
// GET parameters album, song
private static final String URL_SONG = "my url";
// ALL JSON node names
private static final String TAG_NAME = "name";
private static final String TAG_PRICE = "price";
private static final String TAG_ALBUM = "name"; // album
...
try {
JSONObject jObj = new JSONObject(json);
if(jObj != null){
song_name = jObj.getString(TAG_NAME);
album_name = jObj.getString(TAG_ALBUM);
price = jObj.getString(TAG_PRICE);
}
}
catch (JSONException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
我的json网址:
{
"id": "551"
"name": "Rent Disk"
"price": "3233333"
"album_id": "3"
"album": "Michael"
}
Run Code Online (Sandbox Code Playgroud)
如何解决此异常?非常感谢 !!
您可以在存入价值之前先进行这样的检查
JSONObject jObj = new JSONObject(json);
if(jObj != null) {
if(jObj.has(TAG_NAME)) {
song_name = jObj.getString(TAG_NAME);
}
if(jObj.has(TAG_ALBUM)){
album_name = jObj.getString(TAG_ALBUM);
}
if(jObj.has(TAG_PRICE)){
price = jObj.getString(TAG_PRICE);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2344 次 |
| 最近记录: |