从Json反序列化抛出异常

时间:2017-11-15 05:40:53

标签: 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, \"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)

我不知道代码有什么问题。我的模型参数类有什么问题吗?我的模型参数类中也有非常嵌套的集合..

2 个答案:

答案 0 :(得分:0)

看起来你的json文本包含一个ModelParameter对象数组而不是一个对象。所以要么你需要

  • 删除json字符串开头和结尾的[]个字符,使其成为json对象
  • 或将其解析为ModelParameter个对象的数组,如下所示

ModelParameter[] modelParam = gson.fromJson(reader,ModelParameter[].class);

答案 1 :(得分:0)

正如@Udith所述,输入文本是一个ModelParameter类的数组,所以要解决你需要从json字符串的开头和结尾删除[]或更改下面的阅读器代码所需的问题

ModelParameter[] modelParams = gson.fromJson(reader, ModelParameter[].class);

这将从输入文本中为您提供modelParameters数组。