0

I am using JSONObject to convert this xml to a Json String. Then I am using Gson to convert this Json String to POJO.

This XML:

<?xml version="1.0" encoding="UTF-8"?>
<History total="2" pageSize="2" pageStart="0">
    <Asset href="/SomePath/continues" id="Story:12345:6789">
        <Attribute name="Number">S-12345</Attribute>
        <Attribute name="ChangeDate">2017-02-06T15:50:00.180</Attribute>
        <Attribute name="Status.Name">Done</Attribute>
        <Attribute name="Owners.Name">
            <Value>Joe Smith</Value>
        </Attribute>
    </Asset>
    <Asset href="/SomePath/continues" id="Story:9876:54321">
        <Attribute name="Number">S-67890</Attribute>
        <Attribute name="ChangeDate">2017-04-04T09:46:10.780</Attribute>
        <Attribute name="Status.Name">Done</Attribute>
        <Attribute name="Owners.Name">
            <Value>Joe Smith</Value>
            <Value>Tom Fong</Value>
        </Attribute>
    </Asset>
</History>

Becomes This:

{  
   "History":{  
      "total":2,
      "pageStart":0,
      "pageSize":2,
      "Asset":[  
         {  
            "Attribute":[  
               {  
                  "name":"Number",
                  "content":"S-12345"
               },
               {  
                  "name":"ChangeDate",
                  "content":"2017-02-06T15:50:00.180"
               },
               {  
                  "name":"Status.Name",
                  "content":"Done"
               },
               {  
                  "name":"Owners.Name",
                  **"Value":"Joe Smith"**
               }
            ],
            "href":"/SomePath/continues",
            "id":"Story:12345:6789"
         },
         {  
            "Attribute":[  
               {  
                  "name":"Number",
                  "content":"S-67890"
               },
               {  
                  "name":"ChangeDate",
                  "content":"2017-04-04T09:46:10.780"
               },
               {  
                  "name":"Status.Name",
                  "content":"Done"
               },
               {  
                  "name":"Owners.Name",
                  **"Value":[  
                     "Joe Smith",
                     "Tom Fong"
                  ]**
               }
            ],
            "href":"/SomePath/continues",
            "id":"Story:9876:54321"
         }
      ]
   }
}

The Problem arises with the ("Value") tags. Sometimes they are just a string and other times it's an array.

I have been reading about TypeTokens in Gson, is this the only way to deserialize this inconsistent data type?

My Classes Are Set Up Like this:

public class QueryResults{
History History;
//Get/Sets
}

public class History{
    long pageSize;
    int pageStart;
    long total;
    List<Asset> Asset;
}

public class Asset{
    String href;
    String id;  
    List<Attribute> Attribute;
}

public class Attribute{
    private String name;
    private String content;
    private Value value;
}

private class Value{
    private List<String> names;
    private String name;
}

I have tried including (as shown) both a String variable and a List variable in the Value class. I then have a null check hoping that gson would choose the correct one, but that did not work.

Any suggestions for this that I am completely missing?

I should note.. I do NOT have control over this data.

0 Answers0