2016-03-18 13 views
2

この形式のJson Stringを使用しています。BEGIN_OBJECTが必要ですが、1行目の列13のBEGIN_ARRAYでした

{ 
    "columns": ["One", "Two", "Three"], 
    "columnNames": ["One", "Two", "Three"], 
    "isCTemplate": [true, true, true], 
    "isCrequired": [false, false, false], 
    "columnSizes": [10, 50, -1], 
    "columnFormats": ["TEXT", "TEXT", "BOOLEAN(true/false)"], 
    "parseErrors": null, 
    "data": [ 
     ["a", "f", "8"], 
     ["b", "e", "82"], 
     ["c", "d", "822"] 
    ], 
    "numberOfRecordsInserted": 0, 
    "dataErrors": [null, null, null], 
    "dataWarnings": ["1", "2", "3"], 
    "templateCH": ["One", "Two", "Three"], 
    "importS": [null, null, null], 
    "formatTypeMap": { 
     "array": "TEXT", 
     "boolean": "BOOLEAN(true/false)", 
     "currency": "CURRENCY" 
    }, 
    "TypeId": "acc", 
    "isColumn": [false, false, false], 
    "prefixed": [], 
    "missed": "tEST", 
    "duplicateS": null, 
    "global": null, 
    "y": 0, 
    "s": 1, 
    "m": 2, 
    "f": 3, 
    "vStatus": { 
     "vRows": 5, 
     "v": 5 
    } 
} 

マイJsonhelperクラス、

public class CopyOfTViewsJSONS { 
    @JsonProperty("columns") 
    public List<String> columns=new ArrayList<String>(); 

    @JsonProperty("columnNames") 
    public List<String> columnNames=new ArrayList<String>(); 

    @JsonProperty("isCTemplate") 
    public List<String> isCTemplate=new ArrayList<String>(); 

    @JsonProperty("isCrequired") 
    public List<String> isCrequired=new ArrayList<String>(); 

    @JsonProperty("columnSizes") 
    public List<String> columnSizes=new ArrayList<String>(); 

    @JsonProperty("columnFormats") 
    public List<String> columnFormats=new ArrayList<String>(); 

    @JsonProperty("parseErrors") 
    public List<String> parseErrors=new ArrayList<String>(); 

    @JsonProperty("data") 
    public List<List<String>> data=new ArrayList<List<String>>(); 

    @JsonProperty("numberOfRecordsInserted") 
    public int numberOfRecordsInserted; 

    @JsonProperty("dataErrors") 
    public List<String> dataErrors=new ArrayList<String>(); 

    @JsonProperty("dataWarnings") 
    public List<String> dataWarnings=new ArrayList<String>(); 

    @JsonProperty("templateCH") 
    public List<String> templateCH=new ArrayList<String>(); 

    @JsonProperty("importS") 
    public List<String> importS=new ArrayList<String>(); 

    @JsonProperty("formatTypeMap") 
    public HashMap<String, String> formatTypeMap=new HashMap<String, String>(); 

    @JsonProperty("TypeId") 
    public String TypeId; 

    @JsonProperty("prefixed") 
    public List<String> prefixed=new ArrayList<String>(); 

    @JsonProperty("missed") 
    public List<String> missed=new ArrayList<String>(); 

    @JsonProperty("duplicateS") 
    public List<String> duplicateS=new ArrayList<String>(); 

    @JsonProperty("global") 
    public List<String> global=new ArrayList<String>(); 

    @JsonProperty("Y") 
    public int Y; 

    @JsonProperty("s") 
    public int s; 

    @JsonProperty("m") 
    public int m; 

    @JsonProperty("f") 
    public int f; 

    @JsonProperty("vStatus") 
    public HashMap<String, String> vStatus=new HashMap<String, String>(); 


    } 

と私はHashMap<String,CopyOfTViewsJSONS>このようにそのJSON文字列を代入しようとしています、

HashMap<String,CopyOfTViewsJSONS> mapsss = new Gson().fromJson(tmp, new TypeToken<HashMap<String, CopyOfTViewsJSONS >>(){}.getType()); 

しかし、私はjava.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 13<br />com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 13 Exception.

Iを取得していますgsonライブラリでjsonを初めて使用しています。

この問題で私を助けてくれる人はいますか?

おかげ

+0

は、この 'Gsonのgson =新しいGsonBuilderをお試しください:

@JsonProperty("missed") public String missed; 

あなたはそれを正しくデシリアライズされたことを確認するために、結果のいくつかのプロパティをプリントアウトすることができます:String修正にこの問題をクラスフィールドを変更します).setLenient()。create(); ' –

+0

@Matthewはhashmapに代入する前にこれを追加する必要がありますか? – Sandy

+0

これは、 'new Gson().Json(...' gson.fromJson(tmp、... ')の代わりに使用します。 –

答えて

3

は、なぜあなたはMap<String, CopyOfTViewsJSONS>にJSONをデシリアライズしようとしていますか?あなたが投稿したクラスとJSONからわかる限り、JSONはCopyOfTViewsJSONSクラスのインスタンスを完全に表しています。そのため、このようにそれをデシリアライズすることができるはずです。

CopyOfTViewsJSONS result = new Gson().fromJson(tmp, CopyOfTViewsJSONS.class); 

しかし、"missed"プロパティが誤って自分のクラスにマッピングされているので、これに若干の問題があります。 JSONの例では、"missed"Stringですが、クラスではList<String>です。 (

System.out.println(result.columns);     // prints [One, Two, Three] 
System.out.println(result.numberOfRecordsInserted); // prints 0 
System.out.println(result.TypeId);     // prints acc 
System.out.println(result.vStatus);     // prints {v=5, vRows=5} 
関連する問題