(прошло всего 7,5 месяцев.)
Проблема в том, что структура класса, которую вы пытаетесь десериализовать, не соответствует структуре JSON.это работает для десериализации JSON при условии.Выход составляет
[Metrics: [
[MetricContainer: [Metric: type=1, name=slide-11-start, value=1287249598295, sessionID=]],
[MetricContainer: [Metric: type=1, name=slide-21-start, value=1287249601368, sessionID=]],
[MetricContainer: [Metric: type=7, name=resolution, value=1680x1050, sessionID=]],
[MetricContainer: [Metric: type=6, name=OS, value=Linux, sessionID=]],
[MetricContainer: [Metric: type=5, name=browser, value=Netscape, sessionID=]]]]
public class Foo
{
static String jsonInput =
"{" +
"\"metrics\":" +
"[" +
"{" +
"\"metric\":" +
"{" +
"\"type\":1," +
"\"name\":\"slide-11-start\"," +
"\"value\":\"1287249598295\"," +
"\"sessionID\":\"\"" +
"}" +
"}," +
"{" +
"\"metric\":" +
"{" +
"\"type\":1," +
"\"name\":\"slide-21-start\"," +
"\"value\":\"1287249601368\"," +
"\"sessionID\":\"\"" +
"}" +
"}," +
"{" +
"\"metric\":" +
"{" +
"\"type\":7," +
"\"name\":\"resolution\"," +
"\"value\":\"1680x1050\"," +
"\"sessionID\":\"\"" +
"}" +
"}," +
"{" +
"\"metric\":" +
"{" +
"\"type\":6," +
"\"name\":\"OS\"," +
"\"value\":\"Linux\"," +
"\"sessionID\":\"\"" +
"}" +
"}," +
"{" +
"\"metric\":" +
"{" +
"\"type\":5," +
"\"name\":\"browser\"," +
"\"value\":\"Netscape\"," +
"\"sessionID\":\"\"" +
"}" +
"}" +
"]" +
"}";
public static void main(String[] args)
{
GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.create();
Metrics metrics = gson.fromJson(jsonInput, Metrics.class);
System.out.println(metrics);
}
}
class Metrics
{
private List<MetricContainer> metrics;
@Override
public String toString()
{
return String.format(
"[Metrics: %1$s]",
metrics);
}
}
class MetricContainer
{
private Metric metric;
@Override
public String toString()
{
return String.format(
"[MetricContainer: %1$s]",
metric);
}
}
class Metric
{
private int type;
private String name;
private String value;
private String sessionID;
@Override
public String toString()
{
return String.format(
"[Metric: type=%1$d, name=%2$s, value=%3$s, sessionID=%4$s]",
type, name, value, sessionID);
}
}