首页 > 解决方案 > JSONObject["platformTypeCode"] 未找到

问题描述

当我尝试从 JSONArray 获取 JSONObject 时,出现以下错误:

JSONObject["platformTypeCode"] 未找到,问题是我需要 platformTypeCode 的值

我的代码如下:

String value = null;
    
        
        JSONArray packagingList = (packagingInformationModule.has("packaging")) ? packagingInformationModule.getJSONArray("packaging") : null;
        if (packagingList!=null) {
                int i=0;
                while(i<packagingList.length()) {
                    JSONObject platform = ((JSONObject)packagingList.get(i));
                    JSONObject platformTypeCode = platform.getJSONObject("platformTypeCode");
                    value = platformTypeCode.getString("value");
                    
                    i++;
                }
            }

我的 Json 如下:

packagingInformationModule": {
                    "packaging": [
                      {
                        "packagingTypeCode": {
                          "value": "AM"
                        },
                        "platformTypeCode": {
                          "value": "48"
                        },
                        "packagingMaterial": [
                          {
                            "packagingMaterialTypeCode": {
                              "value": "FIBRE_BURLAP"
                            },
                            "packagingMaterialCompositionQuantity": [
                              {
                                "value": 2,
                                "measurementUnitCode": "KGM"
                              }
                            ]
                          }
                        ]
                      }
                    ]
                  }

我找不到问题所在,因为当我使用另一个具有相同结构的 Json 进行测试时它正在工作......

标签: javajson

解决方案


推荐阅读