我有一个JSON格式的对象,我需要读取它并将其放在该类的Object中。怎么??
mainResult = {"metricsId":1161,"metricsName":"Headcount edit","metricsDesc":"Number of employees","createdOn":"Nov 22, 2013 12:00:00 AM","createdBy":"madhuc23@in.ibm.com","lastComputed":"Mar 14, 2014 12:00:00 AM","upadateFrequency":"Weekly"}
我做的是......
MetricsMaster newmetricsMasterData = json.fromJson(
metricsMaster, MetricsMaster.class);
System.out.println("newmetricsMasterData== " + newmetricsMasterData);
但是有例外,
SEVERE: Servlet.service() for servlet [appServlet] in context with path [/SynergyWeb] threw exception [Request processing failed;
nested exception is com.google.gson.JsonParseException:
The JsonDeserializer StringTypeAdapter failed to deserialize json object
{"metricsName":"Headcount edit","metricsType":"Statistics","metricsId":0,"actulaVariables":[{"variableName":"TotalEmps","criteria":"SELECT COUNT( DISTINCT EMP_NO) from SYNERGYRAWDATA.BLUEPAGE","classification":"info","computeTermType":"Weekly","datasource":"Bluepage","dataSourceType":"DataSource","effectiveDate":"Select","attribute":{"Bluepage.EMP_NO":"Count"},"day":0,"month":0,"week":0}],"actualExpression":{"expression":"TotalEmps"},"normalizedResultExpression":{"expression":"actualValue"},"datasets":["Bluepage"],"dependencyMetrics":[],"computationType":"Weekly"}
given the type class java.lang.String] with root cause
Throwable occurred: java.lang.UnsupportedOperationException
at com.google.gson.JsonElement.getAsString(JsonElement.java:184)
at com.google.gson.DefaultTypeAdapters$StringTypeAdapter.deserialize(DefaultTypeAdapters.java:968)
at com.google.gson.DefaultTypeAdapters$StringTypeAdapter.deserialize(DefaultTypeAdapters.java:960)
at com.google.gson.JsonDeserializerExceptionWrapper.deserialize(JsonDeserializerExceptionWrapper.java:51)
提前致谢
答案 0 :(得分:0)
错误意味着Gson无法将JSON中的值放入Java实例,因为Java实例需要String,但JSON在该位置不包含String。
我建议你编写一个测试用例,它接受JSON字符串并尝试将其转换为MetricsMaster
的实例。
这样,您可以快速连续多次运行代码并修改JSON输入,直到错误消失。