我使用retrofit2 + gson。 我的问题是关于什么时候服务器有数据,它发送给我json像:
{
"next":false,
"total":4,
"start":0,
"rows":[
{
"calls":29,
"new_calls":29,
"new_quality_calls":10,
"item":"MzY4NzA3NDA0Mw",
"sessions":3,
"calls_per":966.66666666666663
},
{
"calls":15,
"new_calls":15,
"new_quality_calls":5,
"item":"test312312312",
"sessions":2,
"calls_per":750
},
{
"calls":0,
"new_calls":0,
"new_quality_calls":0,
"item":"test",
"sessions":4,
"calls_per":0
},
{
"calls":0,
"new_calls":0,
"new_quality_calls":0,
"item":"test2",
"sessions":2,
"calls_per":0
}
],
"summary":{
"sessions":11,
"calls":44,
"new_calls":44,
"new_quality_calls":15,
"calls_per":400
}
}
但是当没有数据时,它会发送给我:
{"next":false,"total":0,"start":0,"rows":[],"summary":[]}
问题是summary
是一个对象或数组是某种情况。
我使用这个网站制作gson calsses http://www.jsonschema2pojo.org/
当我得到数据时它没问题,但是当没有数据时我得到了一个错误:
Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 56 path
如何在没有手动数据解析的情况下通过retroft2 (.addConverterFactory(GsonConverterFactory.create()))
自动解决?
答案 0 :(得分:1)
添加你自己的转换器,它将解析AST(readTree
),检查$.summary
类型,修复它,然后将这个固定的AST提供给TypeAdapter。