0

需要将 json 响应转换为 java pojo,其中字段(在示例中为 'performers' )可以返回为 {} 或 [{},{}] 或 null,如下例所示:

示例1:performer value here is a list of datas '{}'.

{

    title:"Sample title1",
    performers: {
         performer:[{
              creator: "AAAA",
              linker: "XXX",

         },
         {
              creator: "BBBBB",
              linker: "YYY",

         }]
     },

      venue_address: "sample addr1"

}

示例 2:performer value here is just data '{}'


{


    title:"sample title2",
    performers: {
         performer: {
              creator: "AAAA",
              linker: "YYY",
         }
    },
    venue_address: "sample addr2"
}

示例 3:performer value is null here

{

    title:"sample title3",
    performers:null,
    venue_address: "sample addr3"
}

我们如何在gson.fromJson.As的帮助下将此 json 响应转换为 java pojo"Expecting an OBJECT but found is ARRAY"

4

1 回答 1

0

正确的做法是,

Whether or not you have performers or not it should be an array.
JsonArray should be enclosed with []. even you have ZERO or ONE or More perfomer

示例 2:此处的执行者值只是数据 '{}'

{
    title:"sample title2",
    performers: [{
        performer: {
          creator: "AAAA",
          linker: "YYY",
        }]
    },
    venue_address: "sample addr2"
}

示例 3:performer 值在此处为空

{
    title:"sample title3",
    performers:[],
    venue_address: "sample addr3"
}
于 2015-01-17T06:19:15.860 回答