Java 应为BEGIN_对象,但为BAGIN_数组

Java 应为BEGIN_对象,但为BAGIN_数组,java,spring-mvc,Java,Spring Mvc,我需要你们的帮助,我想在字段“foto”中输入一个值,但我得到了一些错误,应该是BEGIN\u对象,但却是BAGIN\u数组。 这是我的刀 @SuppressWarnings("unchecked") public String detinst (String json)throws Exception{ sesi = sf.openSession(); tx = sesi.beginTransaction();

我需要你们的帮助,我想在字段“foto”中输入一个值,但我得到了一些错误,应该是BEGIN\u对象,但却是BAGIN\u数组。 这是我的刀

    @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 * from istlsi_edc_tkn_tebel where 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;
        }
如果程序启动,这将是一个json

 [{..........."phone":"8555555","kde_pos":121212,"sts_edc":0,"foto":""}]
如果我们看看上面的json,字段“foto”是空的,我必须做什么才能变成这样

[{..........."phone":"8555555","kde_pos":121212,"sts_edc":0,"foto":"asanskasndjksnds"}]

您需要从json中删除封闭的“[]”,因为Gson需要的是单个json对象,而不是json对象数组。 将模型列表序列化为JSON:

List<installasimodel> result = query.addEntity(installasimodel.class).list();
String inst = gson.toJson(result); //this serializes a list and the resulting JSON is a JSON array of objects ([{...}]).
因此,您需要更改序列化以序列化列表的第一个对象(假设长度始终为1):
gson.toJson(result.get(0))
或者您需要更改反序列化,以便GSON需要一个列表:

List<installasimodel> result = gson.fromJson(json, new TypeToken<List<installasimodel>>(){}.getType() );
List result=gson.fromJson(json,new-TypeToken(){}.getType());

sory您是指像这样的SQLQuery query=sesi.createSQLQuery(“select*from istlsi\u edc\u tkn\u tebel,其中tid='“+tid+””);List result=gson.fromJson(查询,new-TypeToken(){}.getType());String inst=gson.toJson(结果);我从类型Gson中的json(String,Type)获得的方法不适用于我给出答案的参数(SQLQuery,Type)。我认为query.addEntity(installasimodel.class).list();应始终返回单个结果。如果这是正确的,您只需要将原始解决方案中的一行:“String inst=gson.toJson(result);”更改为“String inst=gson.toJson(result.get(0));”
installasimodel hasil = gson.fromJson(json, installasimodel.class);
List<installasimodel> result = gson.fromJson(json, new TypeToken<List<installasimodel>>(){}.getType() );