Android 获得JSONException?
我试图解析下面的Android 获得JSONException?,android,json,jsonexception,Android,Json,Jsonexception,我试图解析下面的json,并在第150行(用代码标记)捕获了JSONException,但我无法找出它发生的原因。目标是找到特定软件包的测试列表,并将其存储在arraylist中 当我将screen\u包作为第二个参数传递时,日志消息是:Value screen\u包类型java.lang.String不能转换为JSONObject。根据我的理解,我们可以在JSONObject的构造函数中传递String对象。如果我错了,请纠正我。看一看 { "package_name": { "c
json
,并在第150行(用代码标记)捕获了JSONException
,但我无法找出它发生的原因。目标是找到特定软件包的测试列表,并将其存储在arraylist
中
当我将screen\u包作为第二个参数传递时,日志消息是:Value screen\u包类型java.lang.String不能转换为JSONObject。根据我的理解,我们可以在JSONObject
的构造函数中传递String
对象。如果我错了,请纠正我。看一看
{
"package_name": {
"camera_package": {
"RearCamera": {
"test_name": "RearCamera",
"test_type": "Mandatory",
"visibility": "true",
"report_name": "RearCamera",
"test_category": "manual",
"order": "42"
},
"FrontCamera": {
"test_name": "FrontCamera",
"test_type": "Mandatory",
"visibility": "true",
"report_name": "FrontCamera",
"test_category": "manual",
"order": "43"
},
"Flash": {
"test_name": "Flash",
"test_type": "Mandatory",
"visibility": "true",
"report_name": "Flash",
"test_category": "manual",
"order": "1"
},
"AutoFocus": {
"test_name": "AutoFocus",
"test_type": "Mandatory",
"visibility": "true",
"report_name": "AutoFocus",
"test_category": "manual",
"order": "35"
},
"VideoRecord": {
"test_name": "VideoRecord",
"test_type": "mandatory",
"visibility": "true",
"report_name": "VideoRecord",
"test_category": "manual",
"order": "10"
},
"FrontVideoRecorder": {
"test_name": "FrontVideoRecorder",
"test_type": "mandatory",
"visibility": "true",
"report_name": "FrontVideoRecorder",
"test_category": "manual",
"order": "11"
}
},
"screen_package": {
"Screen": {
"test_name": "Screen",
"test_type": "Mandatory",
"visibility": "true",
"report_name": "Screen",
"test_category": "manual",
"order": "21"
}
}
}
}
以下是java代码:
public void parseJSON(String jsonName, String packageName) {
try {
JSONObject jsonRootObject = new JSONObject(jsonName);
if (jsonRootObject != null && jsonRootObject.length() > 0) {
Iterator<String> packageKeys = jsonRootObject.keys();
if (packageKeys != null) {
while(packageKeys.hasNext()){
String currentPackageKey = packageKeys.next();
if(currentPackageKey!= null && currentPackageKey.length()>0 &&
currentPackageKey.equals(packageName)){
JSONObject jsonPackageObject = new JSONObject(currentPackageKey); //line 150
if(jsonPackageObject!=null && jsonPackageObject.length()>0) {
Iterator<String> testNameKeys = jsonPackageObject.keys();
if(testNameKeys!=null){
while(testNameKeys.hasNext()) {
String currentTestNameKey = testNameKeys.next();
if(currentTestNameKey!=null && currentTestNameKey.length()>0) {
//do something
}
}
}
}
}
}
}
}
} catch (JSONException e) {
e.printStackTrace();
System.out.println("Exception caught : "+e.getLocalizedMessage());
}
}
public void parseJSON(字符串jsonName,字符串packageName){
试一试{
JSONObject jsonRootObject=新的JSONObject(jsonName);
if(jsonRootObject!=null&&jsonRootObject.length()>0){
迭代器packageKeys=jsonRootObject.keys();
if(packageKeys!=null){
while(packageKeys.hasNext()){
字符串currentPackageKey=packageKeys.next();
如果(currentPackageKey!=null&¤tPackageKey.length()>0&&
currentPackageKey.equals(packageName)){
JSONObject jsonPackageObject=新JSONObject(currentPackageKey);//第150行
if(jsonPackageObject!=null&&jsonPackageObject.length()>0){
迭代器testNameKeys=jsonPackageObject.keys();
if(testNameKeys!=null){
while(testNameKeys.hasNext()){
String currentTestNameKey=testNameKeys.next();
if(currentTestNameKey!=null&¤tTestNameKey.length()>0){
//做点什么
}
}
}
}
}
}
}
}
}捕获(JSONException e){
e、 printStackTrace();
System.out.println(“捕获异常:+e.getLocalizedMessage());
}
}
在本例中
JSONObject jsonPackageObject = new JSONObject(currentPackageKey);
currentPackageKey是一个密钥字符串。您必须在其中提供JSON格式的字符串
因此,将其替换为
JSONObject jsonPackageObject = jsonRootObject.getJSONObject(currentPackageKey);
指示在JSON处理过程中发生了一些异常
你应该使用
返回按名称映射的值(如果该值存在并且是JSONObject),或
否则抛出
代码结构
JSONObject screen_package = jsonobject.getJSONObject("screen_package");
JSONObject screen_package = jsonobject.getJSONObject("screen_package");