支持不同的对象数据类型和改进
问题描述:
下面是我需要Android应用程序以JSON形式传递给REST API的数据的数据结构的描述。特别值得注意的是“响应”字段,其类型各不相同。有时它是一个布尔值,有时是一个整数,或者它可以是一个字符串或一个整数数组。由于各种原因,服务器API不能轻易改变,所以我必须按原样支持它。支持不同的对象数据类型和改进
如何自定义我的Retrofit用途以迎合数据类型不同的字段?我以前为Retrofit的休息适配器编写了类型适配器,这对于处理响应非常有用,但是请求类型适配器的等价物是什么?
array(
array(
question_id => 1,
route_id => 1,
response => true,
timestamp => ‘2014-01-01 20:01:01'
),
array(
question_id => 2,
route_id => 1,
response => ’this is a string',
timestamp => ‘2014-01-01 20:01:01'
),
array(
question_id => 3,
route_id => 1,
response => 1,
timestamp => ‘2014-01-01 20:01:01'
),
array(
question_id => 4,
route_id => 1,
response => array(
1234,
178,
109
),
timestamp => ‘2014-01-01 20:01:01'
),
)
答
事实证明这非常简单。如果Retrofit在模型类中传递Object类型的对象,它会智能地将它们转换为正确的JSON字段类型。
public class MyModel {
// Renders to JSON as boolean, int, String etc depending on the underlying class
public Object response;
}
的
可能重复【如何处理动态JSON在改造?](http://stackoverflow.com/questions/24279245/how-to-handle-dynamic-json-in-retrofit) – dominik4142 2014-10-17 09:09:45