从Complex Json字符串中获取值

时间:2017-05-12 07:03:39

标签: 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");

仍然没有用。

2 个答案:

答案 0 :(得分:1)

试试这种方式

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

答案 1 :(得分:0)

首先让我们存储对象:

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"