0

I have below 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
            }
         }
      }
   }
}

So i want to derive value of DNBStandardRating from this string.

I tried with various options like

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

Still its not working.

2 Answers 2

1

Try this way

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);
Sign up to request clarification or add additional context in comments.

1 Comment

@user1037452 please accept the answer if it worked for you !
0

First let us store the object:

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}}}}}

We can then use the variable to go down the object path and obtain the value such as the following:

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

This will result in "3A4"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.