繁体   English   中英

从Complex Json字符串获取值

[英]Get Value from Complex Json string

我有下面的字符串。

{
   "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);

首先让我们存储对象:

var standardRating = {"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}}}}}

然后,我们可以使用变量沿对象路径向下移动并获取值,如下所示:

standardRating.OrderProductResponse.OrderProductResponseDetail.Product.Organization.Assessment.DNBStandardRating.DNBStandardRating

这将导致"3A4"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM