![](/img/trans.png)
[英]Android , Deserialize JSON Array that have different types using Jackson
[英]Deserialize JSON Array containing different types using Jackson
我正在尝试反序列化如下所示的JSON数组:
[
"96.99999",
"1",
1378962690
]
有什么方法可以直接序列化这种数组,还是需要创建一个Object []数组,然后在以后将其转换为适当的类型?
任何帮助是极大的赞赏。
您可以轻松地将其反序列化为String数组。
请参见以下示例:
import java.io.IOException;
import java.util.Arrays;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.ArrayType;
public class JacksonProgram {
public static void main(String[] args) throws IOException {
String json = "[\"96.99999\",\"1\",1378962690]";
ObjectMapper mapper = new ObjectMapper();
ArrayType arrayType = mapper.getTypeFactory().constructArrayType(String.class);
String[] values = (String[]) mapper.readValue(json, arrayType);
System.out.println(Arrays.toString(values));
}
}
反序列化后,您可以使用String[] values
数组进行任何所需的操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.