gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 13:11:46 25 4
gpt4 key购买 nike

我有以下字符串。

{
"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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com