gpt4 book ai didi

android - 获取 JSONException?

转载 作者:搜寻专家 更新时间:2023-11-01 09:29:35 27 4
gpt4 key购买 nike

我试图解析下面的 json 并在第 150 行(在代码中标记)捕获了 JSONException,但我无法弄清楚为什么会发生。目标是找到特定包的测试列表并将其存储在 arraylist 中。

当我将 screen_package 作为第二个参数传递时,日志消息是:类型为 java.lang.String 的值 screen_package 无法转换为 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());
}
}

最佳答案

JSONException表示在 JSON 处理 期间发生了一些异常。

你应该使用 getJSONObject(String name) .

JSONObject getJSONObject (String name)

Returns the value mapped by name if it exists and is a JSONObject, or throws otherwise

代码结构

 JSONObject screen_package = jsonobject.getJSONObject("screen_package"); 

关于android - 获取 JSONException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48186971/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com