首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >拿到JSONException了?

拿到JSONException了?
EN

Stack Overflow用户
提问于 2018-01-10 11:56:59
回答 2查看 153关注 0票数 2

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

当我将screen_package作为第二次争论传递时,日志消息是:值screen_package类型的java.lang.String不能转换为JSONObject。根据我的理解,我们可以在String的构造函数中传递JSONObject对象。如果我错了,请纠正我。看一看

代码语言:javascript
复制
{
  "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代码:

代码语言:javascript
复制
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());
        }
    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-01-10 12:04:36

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

您应该使用getJSONObject(String name)

代码语言:javascript
复制
JSONObject getJSONObject (String name)

如果存在且是JSONObject,则返回按名称映射的值,否则抛出

码结构

代码语言:javascript
复制
 JSONObject screen_package = jsonobject.getJSONObject("screen_package"); 
票数 2
EN

Stack Overflow用户

发布于 2018-01-10 12:03:06

在这里

代码语言:javascript
复制
JSONObject jsonPackageObject = new JSONObject(currentPackageKey);

currentPackageKey是String.You必须提供JSON格式字符串的键。

所以把它换成

代码语言:javascript
复制
JSONObject jsonPackageObject = jsonRootObject.getJSONObject(currentPackageKey);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48186971

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档