java - Expected BEGIN_OBJECT but was BAGIN_ARRAY -
i need help, want enter value field "foto" error expected begin_object bagin_array. dao
@suppresswarnings("unchecked") public string detinst (string json)throws exception{ sesi = sf.opensession(); tx = sesi.begintransaction(); system.out.println("--------dao---------"); system.out.println("22222222222"); installasimodel hasil = gson.fromjson(json, installasimodel.class); int tid = hasil.gettid(); system.out.println("tid :"+tid); sqlquery query=sesi.createsqlquery("select * istlsi_edc_tkn_tebel tid='"+tid+"'"); list<installasimodel> result = query.addentity(installasimodel.class).list(); string inst = gson.tojson(result); system.out.println("hasil query :"+inst); //////////////////////////// string intofoto ="asanskasndjksnds"; ///////////////////////////// sesi.close(); return inst; }
this model
........ @column(name="id_spv") private int id_spv; @column(name="own_mrchn") private string own_mrchn; @column(name="phone") private string phone; @column(name="kde_pos") private int kde_pos; @column(name="sts_edc") private int sts_edc; @column(name="ms") private string ms; @column(name="sc") private string sc; @column(name="foto") private string foto; @column(name="ttd") private string ttd; ..............
this json if program start
[{..........."phone":"8555555","kde_pos":121212,"sts_edc":0,"foto":""}]
if @ json above, field "foto" empty, must this
[{..........."phone":"8555555","kde_pos":121212,"sts_edc":0,"foto":"asanskasndjksnds"}]
you need remove enclosing "[]" json, because gson expects single json object, not array of json objects. serialize list of models json:
list<installasimodel> result = query.addentity(installasimodel.class).list(); string inst = gson.tojson(result); //this serializes list , resulting json json array of objects ([{...}]).
but in deserialization code trying deserialize array single model:
installasimodel hasil = gson.fromjson(json, installasimodel.class);
so either need change serialization serialize first object of list (assuming length 1): gson.tojson(result.get(0))
or need change change deserialization gson expects list:
list<installasimodel> result = gson.fromjson(json, new typetoken<list<installasimodel>>(){}.gettype() );
Comments
Post a Comment