【问题标题】:Getting JSONException?得到 JSONException?
【发布时间】:2018-06-19 14:19:20
【问题描述】:

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

当我通过 screen_package 作为第二个参数时,日志消息是:Value screen_package of type java.lang.String cannot be convert to 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());
        }
    }

【问题讨论】:

    标签: android json jsonexception


    【解决方案1】:

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

    你应该使用getJSONObject(String name)

    JSONObject getJSONObject (String name)
    

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

    代码结构

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

    【讨论】:

      【解决方案2】:

      在这

      JSONObject jsonPackageObject = new JSONObject(currentPackageKey);
      

      currentPackageKey 是一个关键字符串。你必须在那里提供 JSON 格式的字符串。

      所以换成

      JSONObject jsonPackageObject = jsonRootObject.getJSONObject(currentPackageKey);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-04
        • 1970-01-01
        • 2016-09-05
        • 2019-05-29
        • 1970-01-01
        相关资源
        最近更新 更多