Android 无法将org.json.simple.JSONObject转换为org.json.JSONObject
当我运行以下代码时Android 无法将org.json.simple.JSONObject转换为org.json.JSONObject,android,json,Android,Json,当我运行以下代码时 JSONObject jsonObject = null; JSONParser parser=new JSONParser(); // this needs the "json-simple" library try { Object obj = parser.parse(responseBody); jsonObject=(JSONObject)obj; } catch(Exception ex) {
JSONObject jsonObject = null;
JSONParser parser=new JSONParser(); // this needs the "json-simple" library
try
{
Object obj = parser.parse(responseBody);
jsonObject=(JSONObject)obj;
}
catch(Exception ex)
{
Log.v("TEST","Exception1: " + ex.getMessage());
}
…在运行时,我在日志输出中看到以下内容:
Exception1: org.json.simple.JSONObject cannot be cast to org.json.JSONObject
我不明白为什么。您导入了错误的类。改变
import org.json.JSONObject;
到
将代码更改为:
org.json.simple.JSONObject jsonObject = null;
JSONParser parser=new JSONParser(); // this needs the "json-simple" library
try
{
Object obj = parser.parse(responseBody);
jsonObject=(org.json.simple.JSONObject)obj;
}
catch(Exception ex)
{
Log.v("TEST","Exception1: " + ex.getMessage());
}
或者,如果您只使用
org.json.simple
library来解析json字符串,那么只需导入org.json.simple.
而不是org.json.JSONObject
如果我想同时使用这两个库,是否有方法强制转换该类型?
org.json.simple.JSONObject jsonObject = null;
JSONParser parser=new JSONParser(); // this needs the "json-simple" library
try
{
Object obj = parser.parse(responseBody);
jsonObject=(org.json.simple.JSONObject)obj;
}
catch(Exception ex)
{
Log.v("TEST","Exception1: " + ex.getMessage());
}