简体   繁体   中英

Parsing a really complicated nested json whose structure is always changing in JAVA

I have a really complicated nested json whose structure is always changing. I want to parse it in JAVA such that I can retrieve any element using the key/field name.

Option 1 - The easiest way to do this would be to convert/parse the json into a JAVA object. I have tried everything (gson, JSONObject, Jackson...) But I cant convert the json unless I have a java class ready and since the json doesn't follow a fixed structure I cant convert it into a JAVA class. Is there any other library that can convert the json to a java object? (Without the need for a pre existing java class to convert the json into)

Option 2 - Or is there a way/library I can use, which if given a portion of the json, the program prints all the elements in the json file. Something like this...

StreetAddressLine
PrimaryTownName : value
CountryISOAlpha2Code :value
TerritoryAbbreviatedName :value
PostalCode : value

{"PrimaryAddress": [        {
          "StreetAddressLine": [{"LineText": "492 Koller St"}],
          "PrimaryTownName": "San Francisco",
          "CountryISOAlpha2Code": "US",
          "TerritoryAbbreviatedName": "CA",
          "PostalCode": "94110",
          "AddressUsageTenureDetail": [{"TenureTypeText":           {
            "@DNBCodeValue": 1129,
            "$": "Rents"
          }}],
          "PremisesUsageDetail": [{"PremisesUsageFunctionDetail": [{"PremisesFunctionText":           {
            "@DNBCodeValue": 12058,
            "$": "Manufacturing"
          }}]}],
          "CountyOfficialName": "San Francisco County",
          "TerritoryOfficialName": "California",
          "CountryGroupName": "North America",
          "GeographicalPrecisionText":           {
            "@DNBCodeValue": 0,
            "$": "Unknown"
          },
          "UndeliverableIndicator": false,
          "MetropolitanStatisticalAreaUSCensusCode": ["San Francisco-Oakland-Hayward CA"],
          "RegisteredAddressIndicator": false,
          "ResidentialAddressIndicator": false
        }]}

Thanks a lot!

Try using json-simple to parse the JSON into a bunch of nested JSONObject (which basically are maps) and JSONArray (which basically are lists) elements and extract the values yourself.

Just a node: PrimaryAddress indicates that there might be a SecondaryAddress as well, so the nesting should not change, otherwise it might be hard to determine things like which address StreetAddressLine belongs to.

In that case the better question would be: why does the structure change that often?

Ok I found a solution! I used the Jackson Parser

First map your jsonString to a JsonNode

JsonNode rootNode = mapper.readValue(jsonString, JsonNode.class);

Update the rootNode to contain the json for the required key:

    rootNode = rootNode.findParent(key);

and then depending on if it is an array or a list handle it seperately:

if(rootNode.path(key).isArray()){
//key is the field in the json that you might be looking for
for (final JsonNode objNode : rootNode) {

     for (Iterator<String> keyArray = objNode.getFieldNames(); keyArray.hasNext();){

         fieldName = keyArray.next();

         fieldValue = objNode.path(fieldName).asText();                 
         if(fieldValue != ""){
             System.out.println(fieldName + " = " + fieldValue);

         }else{
             arrayHandler(objNode,fieldName);
         }
     }
 }

At each iteration check if the resulting JsonNode is an array or a list. If it is a List handle it differently (Just iterate over the key value pairs like this)

for (Iterator<String> keyArray = rootNode.getFieldNames(); keyArray.hasNext();){
        fieldName = keyArray.next();
        fieldValue = rootNode.get(fieldName).asText();
        System.out.println(fieldName + " = " + fieldValue);
    }

After every iteration check what the next jsonNode is and call the respective handler recursively...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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