我正在使用google的GSON解析库,我不确定之前是否曾经问过这个问题(我确实检查了我能找到的内容)但是,我在这里!这是关于fromJSON
方法以及它如何拒绝将某些类解释为数组。
鉴于下面的JSON结构:
{"visualization": {
"root": {
"fullname": "CC/dudu",
"name": "dudu",
"type": "String",
"children": [
{
"fullname": "CC/dudu/lulu",
"name": "lulu",
"type": "String"
}
]
},
"traces": {
"messages": [
{
"from": "dudu",
"method": "call()",
"scenario": "#1",
"timestamp": "09-12-2013 00:21:14",
"to": "dudu",
"type": "void",
"violation": "true",
"visible": "true"
}
],
"scenarios": [
{
"name": "testscenario",
"id": "#1",
"description": "testing parsing!"
}
]
}
}}
随附的课程。
public class Response {
private Visualization visualization;
//+getter/setter
}
public class Visualization {
private Component root;
private Map<String, Trace> traces;
//+getter/setter
}
public class Trace {
private ArrayList<Message> messages;
private ArrayList<Scenario> scenarios;
//+getter/setter
}
我得到的错误是GSON期望一个Object而不是一个数组(在消息的“[”标记之前)。谁知道为什么会这样?类型(可以在类中看到)是List
,所以应该没问题。我已经尝试在数组中有更多的对象,我仍然得到相同的错误消息!为什么Gson将List<TypeA>
类型解释为对象而不是数组?
修改 这是代码代码,但它有点无意义,因为由于解析过程而抛出异常。我怀疑你会找到任何有用的东西。 “visualization”是一个具有正确JSON格式的字符串。
Gson gsonParser = new Gson();
Response r = gsonParser.fromJson(visualization, Response.class);
答案 0 :(得分:1)
下面的代码允许您准确地解析您的JSON。
请注意,我将所有内容放入一个类中,以便您更轻松地进行测试。无论如何,Gson不能很好地处理内部类,除非它们是静态的。所以我建议你为每个类创建一个文件或只使用静态内部类。
package stackoverflow.questions;
import java.util.*;
import com.google.gson.Gson;
public class Q20461706 {
public class Trace {
ArrayList<Message> messages;
ArrayList<Scenario> scenarios;
@Override
public String toString() {
return "Trace [messages=" + messages + ", scenarios=" + scenarios + "]";
}
}
public static class Message {
String from; // "from": "dudu",
String method; // "method": "call()",
String scenario; // "scenario": "#1",
String timestamp; // "timestamp": "09-12-2013 00:21:14",
String to; // "to": "dudu",
String type; // "type": "void",
Boolean violation; // "violation": "true",
Boolean visible; // "visible": "true"
@Override
public String toString() {
return "Message [from=" + from + ", method=" + method + ", scenario=" + scenario + ", timestamp=" + timestamp + ", to=" + to + ", type=" + type + ", violation=" + violation + ", visible=" + visible + "]";
}
}
public static class Scenario {
String name;// "name": "testscenario",
String id; // "id": "#1",
String description; // "description": "testing parsing!"
@Override
public String toString() {
return "Scenario [name=" + name + ", id=" + id + ", description=" + description + "]";
}
}
public static class Component {
String fullname; // "fullname": "CC/dudu",
String name; // "name": "dudu",
String type; // "type": "String",
List<Component> children;
@Override
public String toString() {
return "Component [fullname=" + fullname + ", name=" + name + ", type=" + type + ", children=" + children + "]";
}
}
public static class Visualization {
Component root;
Trace traces;
@Override
public String toString() {
return "Visualization [root=" + root + ", traces=" + traces + "]";
}
}
public static class Response {
Visualization visualization;
@Override
public String toString() {
return "Response [visualization=" + visualization + "]";
}
}
public static void main(String[] args) {
String json =
" {\"visualization\": { "+
" \"root\": { "+
" \"fullname\": \"CC/dudu\", "+
" \"name\": \"dudu\", "+
" \"type\": \"String\", "+
" \"children\": [ "+
" { "+
" \"fullname\": \"CC/dudu/lulu\", "+
" \"name\": \"lulu\", "+
" \"type\": \"String\" "+
" } "+
" ] "+
" }, "+
" \"traces\": { "+
" \"messages\": [ "+
" { "+
" \"from\": \"dudu\", "+
" \"method\": \"call()\", "+
" \"scenario\": \"#1\", "+
" \"timestamp\": \"09-12-2013 00:21:14\", "+
" \"to\": \"dudu\", "+
" \"type\": \"void\", "+
" \"violation\": \"true\", "+
" \"visible\": \"true\" "+
" } "+
" ], "+
" \"scenarios\": [ "+
" { "+
" \"name\": \"testscenario\", "+
" \"id\": \"#1\", "+
" \"description\": \"testing parsing!\" "+
" } "+
" ] "+
" } "+
" }} ";
Gson gsonParser = new Gson();
Response r = gsonParser.fromJson(json, Response.class);
System.out.println(r);
}
}
要回答您的问题,请注意我已使用Visualization
更改了Trace traces
课程,因为您的JSON中只有Trace
个对象,而不是数组。这就是Gson抱怨的原因。
另请注意,我避免将日期解析为日期,您需要为其指定自定义日期格式,但这超出了此问题的范围。你可以在SO上找到很多例子。