java - 从复杂的 Json 字符串中获取值

标签 java arrays json

我有以下字符串。

{
   "OrderProductResponse":{
      "@ServiceVersionNumber":"5.0",
      "TransactionDetail":{
         "ServiceTransactionID":"Id-70e1fecb52ff474c207b0000-2",
         "TransactionTimestamp":"2014-02-15T05:54:10.834-05:00"
      },
      "TransactionResult":{
         "SeverityText":"Information",
         "ResultID":"CM000",
         "ResultText":"Success"
      },
      "OrderProductResponseDetail":{
         "InquiryDetail":{
            "DUNSNumber":"804735132",
            "CountryISOAlpha2Code":"US"
         },
         "Product":{
            "DNBProductID":"RTNG_TRND",
            "Organization":{
               "SubjectHeader":{
                  "DUNSNumber":"804735132"
               },
               "Telecommunication":{
                  "TelephoneNumber":[
                     {
                        "TelecommunicationNumber":"(650) 555-0000",
                        "InternationalDialingCode":"1"
                     }
                  ]
               },
               "Location":{
                  "PrimaryAddress":[
                     {
                        "StreetAddressLine":[
                           {
                              "LineText":"492 Koller St"
                           }
                        ],
                        "PrimaryTownName":"San Francisco",
                        "CountryISOAlpha2Code":"US",
                        "TerritoryAbbreviatedName":"CA",
                        "PostalCode":"94110",
                        "TerritoryOfficialName":"California"
                     }
                  ]
               },
               "OrganizationName":{
                  "OrganizationPrimaryName":[
                     {
                        "OrganizationName":{
                           "$":"Gorman Manufacturing Company, Inc."
                        }
                     }
                  ]
               },
               "OrganizationDetail":{
                  "FamilyTreeMemberRole":[
                     {
                        "FamilyTreeMemberRoleText":{
                           "@DNBCodeValue":12774,
                           "$":"Domestic Ultimate"
                        }
                     },
                     {
                        "FamilyTreeMemberRoleText":{
                           "@DNBCodeValue":12775,
                           "$":"Global Ultimate"
                        }
                     },
                     {
                        "FamilyTreeMemberRoleText":{
                           "@DNBCodeValue":12773,
                           "$":"Parent"
                        }
                     }
                  ]
               },
               "Assessment":{
                  "DNBStandardRating":{
                     "DNBStandardRating":"3A4"
                  },
                  "HistoryRatingText":{
                     "@DNBCodeValue":9078,
                     "$":"Clear"
                  },
                  "FinancialConditionText":{
                     "@DNBCodeValue":415,
                     "$":"Fair"
                  }
               }
            },
            "ArchiveDetail":{
               "PortfolioAssetID":47651715
            }
         }
      }
   }
}

所以我想从这个字符串中获取 DNBStandardRating 的值。

我尝试了各种选项,例如

JSONObject json = new JSONObject(responseString);
JSONArray jsonarr= json.getJSONArray("OrderProductResponse");
String address = jsonarr.getJSONObject(0).getString("DNBStandardRating");

仍然无法正常工作。

最佳答案

试试这个方法

String a = "{\"OrderProductResponse\":{\"@ServiceVersionNumber\":\"5.0\",\"TransactionDetail\":{\"ServiceTransactionID\":\"Id-70e1fecb52ff474c207b0000-2\",\"TransactionTimestamp\":\"2014-02-15T05:54:10.834-05:00\"},\"TransactionResult\":{\"SeverityText\":\"Information\",\"ResultID\":\"CM000\",\"ResultText\":\"Success\"},\"OrderProductResponseDetail\":{\"InquiryDetail\":{\"DUNSNumber\":\"804735132\",\"CountryISOAlpha2Code\":\"US\"},\"Product\":{\"DNBProductID\":\"RTNG_TRND\",\"Organization\":{\"SubjectHeader\":{\"DUNSNumber\":\"804735132\"},\"Telecommunication\":{\"TelephoneNumber\":[{\"TelecommunicationNumber\":\"(650) 555-0000\",\"InternationalDialingCode\":\"1\"}]},\"Location\":{\"PrimaryAddress\":[{\"StreetAddressLine\":[{\"LineText\":\"492 Koller St\"}],\"PrimaryTownName\":\"San Francisco\",\"CountryISOAlpha2Code\":\"US\",\"TerritoryAbbreviatedName\":\"CA\",\"PostalCode\":\"94110\",\"TerritoryOfficialName\":\"California\"}]},\"OrganizationName\":{\"OrganizationPrimaryName\":[{\"OrganizationName\":{\"$\":\"Gorman Manufacturing Company, Inc.\"}}]},\"OrganizationDetail\":{\"FamilyTreeMemberRole\":[{\"FamilyTreeMemberRoleText\":{\"@DNBCodeValue\":12774,\"$\":\"Domestic Ultimate\"}},{\"FamilyTreeMemberRoleText\":{\"@DNBCodeValue\":12775,\"$\":\"Global Ultimate\"}},{\"FamilyTreeMemberRoleText\":{\"@DNBCodeValue\":12773,\"$\":\"Parent\"}}]},\"Assessment\":{\"DNBStandardRating\":{\"DNBStandardRating\":\"3A4\"},\"HistoryRatingText\":{\"@DNBCodeValue\":9078,\"$\":\"Clear\"},\"FinancialConditionText\":{\"@DNBCodeValue\":415,\"$\":\"Fair\"}}},\"ArchiveDetail\":{\"PortfolioAssetID\":47651715}}}}}";
    JSONObject json = new JSONObject(a);

    Object value = json.getJSONObject("OrderProductResponse")
        .getJSONObject("OrderProductResponseDetail").getJSONObject("Product")
        .getJSONObject("Organization")
        .getJSONObject("Assessment").getJSONObject("DNBStandardRating").get("DNBStandardRating");

    System.out.println(value);

关于java - 从复杂的 Json 字符串中获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43931360/

相关文章:

java - 使用 Jackson 自定义反序列化 JSON 字段

java - 使用 Apache Batik 缩放 SVG 图像时如何忽略比例?

java - 用 JMS 模拟串行总线?

Java:通过套接字向/从用 C 编码的程序发送/接收 int 数组

java - 如何序列化 Jackson Joda 日期格式?

javascript - 如果 JSON 对象的属性具有诸如 option1、option2 之类的名称,那么访问 JSON 对象的属性的更好方法是什么

java - JTable数据更新闪烁,客户端-服务器模型

java - 需要排列组合

javascript - PHP 中的 HTML 数组字段解析

c++ - 使用指向数组的指针遍历对象数组