Java 从Json抛出异常反序列化
我正在我的应用程序中阅读JSOn文本,如下所示,我还替换了特殊字符Java 从Json抛出异常反序列化,java,json,Java,Json,我正在我的应用程序中阅读JSOn文本,如下所示,我还替换了特殊字符 Gson gson = new Gson(); JsonReader reader = new JsonReader(new StringReader("[{ \"alg\":\"rf\", \"response_variable\":\"04faa082-b19b-45c1-a0ac-f20794a8ad42\", \"regressionparam\":{ \"maxIterations\":10, \"ob
Gson gson = new Gson();
JsonReader reader = new JsonReader(new StringReader("[{ \"alg\":\"rf\", \"response_variable\":\"04faa082-b19b-45c1-a0ac-f20794a8ad42\", \"regressionparam\":{ \"maxIterations\":10, \"objReg\":0.3, \"ntrees\":35, \"nbins\":10, \"maxDepth\":5, \"scoreTreeInterval\":10, \"epochs\":0.0, \"hidden\":[ 200, 200 ], \"sparse\":true, \"activation\":\"Tanh\", \"stopping_metric\":\"AUTO\", \"stopping_tolerance\":0.001, \"max_runtime_secs\":0.0, \"overwrite_with_best_model\":true, \"missing_values_handling\":\"MeanImputation\", \"weight_column\":\"\", \"standardize\":true, \"score_interval\":10, \"ignore_const_cols\":true, \"distribution\":\"AUTO\", \"number_of_similiar_clusters\":1, \"estimate_k\":false, \"family\":\"gaussian\" }, \"aggParam\":{ \"nodeId\":\"d7c3d762-6078-4f98-a5a4-cc164c76fb34\", \"features\":[ { \"fnodeid\":\"99d63623-1c99-4272-8d59-f836226500a5\", \"nodename\":\"ALM_EN\", \"sensordata_type\":\"ANALOG\" }, { \"fnodeid\":\"be090fd2-2274-453e-a853-6bad53468b90\", \"nodename\":\"HI_SP\", \"sensordata_type\":\"ANALOG\" }, { \"fnodeid\":\"7b3b9f67-ea86-43ba-9fe5-0ed2d446d700\", \"nodename\":\"LOLO_SP\", \"sensordata_type\":\"ANALOG\" }, { \"fnodeid\":\"7e311fca-28b1-4555-be86-c756c96d8794\", \"nodename\":\"LO_SP\", \"sensordata_type\":\"ANALOG\" } ], \"dataTypeToFeatureMap\":{ \"ANALOG\":[ { \"fnodeid\":\"99d63623-1c99-4272-8d59-f836226500a5\", \"nodename\":\"ALM_EN\", \"sensordata_type\":\"ANALOG\" }, { \"fnodeid\":\"be090fd2-2274-453e-a853-6bad53468b90\", \"nodename\":\"HI_SP\", \"sensordata_type\":\"ANALOG\" }, { \"fnodeid\":\"7b3b9f67-ea86-43ba-9fe5-0ed2d446d700\", \"nodename\":\"LOLO_SP\", \"sensordata_type\":\"ANALOG\" }, { \"fnodeid\":\"7e311fca-28b1-4555-be86-c756c96d8794\", \"nodename\":\"LO_SP\", \"sensordata_type\":\"ANALOG\" } ] }, \"predictor\":{ \"fnodeid\":\"04faa082-b19b-45c1-a0ac-f20794a8ad42\", \"nodename\":\"PV\", \"sensordata_type\":\"ANALOG\" }, \"starttime\":1491235200000, \"endtime\":1506700800000, \"samplingRate\":10, \"samplingTimePeriod\":\"MINUTE\", \"aggregator\":\"AVG\" }, \"keyspace\":\"vroc\", \"phoenixHost\":\"auper01-01-20-01-0.prod.vroc.com.au:2181:/hbase-unsecure\", \"number_of_features\":1, \"feature_selection\":false, \"model_parameters_optimization\":false, \"training_ratio\":0.5, \"validation_ratio\":0.7, \"ttf_calculation\":false, \"ttf_threshold\":0.0, \"ttf_thershold_greater\":false, \"number_of_lags\":5, \"modelMetadata\":{ \"startedByUser\":\"Livy Demo\", \"userOrganization\":\"Livy Demo\", \"nodeName\":\"VROC.Rishi_trial.Update_test.try1_1\" } }]"));
ModelParameter modelParam = gson.fromJson(reader, ModelParameter.class);
模型参数类别如下所示:
package au.com.vroc.mdm.model;
import java.io.Serializable;
import com.google.gson.Gson;
/**
* The Class ModelParameter.
*/
public class ModelParameter implements Serializable {
/** The Constant serialVersionUID. */
private static final long serialVersionUID = -8395899340046386755L;
/** The alg. */
public String alg = "rf";
/** The response_variable. */
public String response_variable = "";
/** The regressionparam. */
public RegressionParameter regressionparam = new RegressionParameter();
/** The agg param. */
public AggregationParameter aggParam = new AggregationParameter();;
/** The keyspace. */
public String keyspace = "vroctest";
/** The keyspace. */
public String phoenixHost = null;
/** The automatic feature selection. */
public Integer number_of_features =10 ;
/** Feature selection. */
public Boolean feature_selection =false ;
/** The model parameter optimization. */
public Boolean model_parameters_optimization=false;
/** Training ratio. */
public Double training_ratio=0.5;
/** Validation ratio. */
public Double validation_ratio=0.7;
/** TTF calculation. */
public Boolean ttf_calculation=false;
/** TTF threshold. */
public Double ttf_threshold=0.5;
/** Values greater than TTF threshold become alarms otherwise values below threshold become alarm. */
public Boolean ttf_thershold_greater=true;
/** Number of lags. */
public Integer number_of_lags=1;
public ModelMetadata modelMetadata;
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return new Gson().toJson(this);
}
}
但是,它会引发如下异常:
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:176)
at com.google.gson.Gson.fromJson(Gson.java:803)
at au.com.vroc.mdm.SparkSubmitModel.main(SparkSubmitModel.java:404)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
我不知道代码有什么问题。我的模型参数类有什么问题吗?我的模型参数类中也有非常嵌套的集合。看起来您的json文本包含一个ModelParameter对象数组,而不是单个对象。所以要么你需要
Gson gson = new Gson();
JsonReader reader = new JsonReader(new StringReader("[{ \"alg\":\"rf\", \"response_variable\":\"04faa082-b19b-45c1-a0ac-f20794a8ad42\", \"regressionparam\":{ \"maxIterations\":10, \"objReg\":0.3, \"ntrees\":35, \"nbins\":10, \"maxDepth\":5, \"scoreTreeInterval\":10, \"epochs\":0.0, \"hidden\":[ 200, 200 ], \"sparse\":true, \"activation\":\"Tanh\", \"stopping_metric\":\"AUTO\", \"stopping_tolerance\":0.001, \"max_runtime_secs\":0.0, \"overwrite_with_best_model\":true, \"missing_values_handling\":\"MeanImputation\", \"weight_column\":\"\", \"standardize\":true, \"score_interval\":10, \"ignore_const_cols\":true, \"distribution\":\"AUTO\", \"number_of_similiar_clusters\":1, \"estimate_k\":false, \"family\":\"gaussian\" }, \"aggParam\":{ \"nodeId\":\"d7c3d762-6078-4f98-a5a4-cc164c76fb34\", \"features\":[ { \"fnodeid\":\"99d63623-1c99-4272-8d59-f836226500a5\", \"nodename\":\"ALM_EN\", \"sensordata_type\":\"ANALOG\" }, { \"fnodeid\":\"be090fd2-2274-453e-a853-6bad53468b90\", \"nodename\":\"HI_SP\", \"sensordata_type\":\"ANALOG\" }, { \"fnodeid\":\"7b3b9f67-ea86-43ba-9fe5-0ed2d446d700\", \"nodename\":\"LOLO_SP\", \"sensordata_type\":\"ANALOG\" }, { \"fnodeid\":\"7e311fca-28b1-4555-be86-c756c96d8794\", \"nodename\":\"LO_SP\", \"sensordata_type\":\"ANALOG\" } ], \"dataTypeToFeatureMap\":{ \"ANALOG\":[ { \"fnodeid\":\"99d63623-1c99-4272-8d59-f836226500a5\", \"nodename\":\"ALM_EN\", \"sensordata_type\":\"ANALOG\" }, { \"fnodeid\":\"be090fd2-2274-453e-a853-6bad53468b90\", \"nodename\":\"HI_SP\", \"sensordata_type\":\"ANALOG\" }, { \"fnodeid\":\"7b3b9f67-ea86-43ba-9fe5-0ed2d446d700\", \"nodename\":\"LOLO_SP\", \"sensordata_type\":\"ANALOG\" }, { \"fnodeid\":\"7e311fca-28b1-4555-be86-c756c96d8794\", \"nodename\":\"LO_SP\", \"sensordata_type\":\"ANALOG\" } ] }, \"predictor\":{ \"fnodeid\":\"04faa082-b19b-45c1-a0ac-f20794a8ad42\", \"nodename\":\"PV\", \"sensordata_type\":\"ANALOG\" }, \"starttime\":1491235200000, \"endtime\":1506700800000, \"samplingRate\":10, \"samplingTimePeriod\":\"MINUTE\", \"aggregator\":\"AVG\" }, \"keyspace\":\"vroc\", \"phoenixHost\":\"auper01-01-20-01-0.prod.vroc.com.au:2181:/hbase-unsecure\", \"number_of_features\":1, \"feature_selection\":false, \"model_parameters_optimization\":false, \"training_ratio\":0.5, \"validation_ratio\":0.7, \"ttf_calculation\":false, \"ttf_threshold\":0.0, \"ttf_thershold_greater\":false, \"number_of_lags\":5, \"modelMetadata\":{ \"startedByUser\":\"Livy Demo\", \"userOrganization\":\"Livy Demo\", \"nodeName\":\"VROC.Rishi_trial.Update_test.try1_1\" } }]"));
ModelParameter modelParam = gson.fromJson(reader, ModelParameter.class);
- 删除json字符串开头和结尾的
字符,使其成为json对象[]
- 或者将其解析为
对象数组,如下所示ModelParameter
ModelParameter[]modelParam=gson.fromJson(读取器,ModelParameter[].class)代码>如@Udith input text所述,是一个ModelParameter类数组,因此要解决此问题,您需要从json字符串的开头和结尾删除[]
,或者如下更改读卡器代码
ModelParameter[] modelParams = gson.fromJson(reader, ModelParameter[].class);
这将为您提供输入文本中的模型参数数组。如果以下答案解决了您的问题,请标记为已接受。