java - Gson:预期为 begin_array,但为 STRING 如何控制它

标签 java json rest gson

我正在学习如何在 REST 服务中生成和使用 JSON,但我想学好它,所以我尝试了所有可能的对象情况,其中之一是具有像此类这样的 List 属性的对象:

import java.util.List;

public class PruebaJSON {

    private String nombre;
    private List atributos;
    private String descripcion;
    public String getNombre() {
        return nombre;
    }
    public void setNombre(String nombre) {
        this.nombre = nombre;
    }
    public List getAtributos() {
        return atributos;
    }
    public void setAtributos(List atributos) {
        this.atributos = atributos;
    }
    public String getDescripcion() {
        return descripcion;
    }
    public void setDescripcion(String descripcion) {
        this.descripcion = descripcion;
    }   
}

那么我在休息服务方法上所做的就是这样:

@POST
@Path("/prueba")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public PruebaJSON prueba(String data) {

    try {

        JSONObject json = new JSONObject(data);


        Gson convertir = new GsonBuilder().create();
        PruebaJSON pruebaJson = convertir.fromJson(json.toString(), PruebaJSON.class);

        return pruebaJson;
    } catch (Exception e) {
        System.out.println("error " + e);
        return null;
    }

}

然后在 POSTMAN 中我传递了这个:

{
    "descripcion": "Primera prueba",
    "nombre": "Prueba 1",
    "atributos": [
        "hello",
        "kek",
        "lul"
    ]
}

它工作得很好,问题是当我尝试用 Java 做同样的事情时,例如:

List atributos = new ArrayList<>();
atributos.add("hello");
atributos.add("kek");
atributos.add("lul");

System.out.println(bus.prueba("Prueba 1", "Primera Prueba", atributos));

bus.prueba 只是执行服务,但随后在控制台中我收到此错误:

14:16:56,567 INFO  [stdout] (default task-2) error com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 66 path $.atributos

我搜索了错误并发现了这个: Gson: Expected begin_array but was STRING

我理解错误,但是解决方案是什么? 我无法真正控制 JSON 如何构建数组列表,可以吗? 这是我的客户端中的方法 prueba:

public String prueba(String nombre, String descripcion, List atributos) {
        HashMap map = new HashMap<>();

        map.put("nombre", nombre);
        map.put("descripcion", descripcion);
        map.put("atributos", atributos);
        String respuesta = utilidadesRestSeguridad.consumir("prueba", map);
        return respuesta;

    }

在我的客户端组件中,这是构建 json 的方法:

public static JsonObject generateJSON(HashMap map) throws MalformedURLException {
    JsonObject json = new JsonObject();
    for (Object key : map.keySet()) {
        json.addProperty(key.toString(), map.get(key).toString());
    }

    return json;

}

就是这样,如果您想查看更多代码或我解释一些内容,请告诉我,我感谢任何帮助。

我认为由于 .toString() 的原因,错误可能出在方法generateJSON中,但是我应该如何处理这种情况呢?

最佳答案

假设 utilidadesRestSeguridad.consumir("prueba", map) 行最终调用下游的 generateJSON 方法,那么您的问题可能出现在 generateJSON 中() 方法如您所怀疑。基本上,您只需将所有元素添加为字符串。如果您的 map 中的元素之一是 List 的实例,那么您需要调用 JsonObject#add("atributos", value) 。例如,您将需要类似以下代码的内容:

if (map.get(key) instanceof List) {
    json.add(key.toString(), map.get(key);
} else {
    json.addProperty(key.toString(), map.get(key).toString());
}

关于java - Gson:预期为 begin_array,但为 STRING 如何控制它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56569084/

相关文章:

java - firebase 在家庭 Activity 内切换 Activity 时导航到登录 Activity

java - SSL 和证书 keystore

php - 使用 PHP、curl 解码从 Flickr API 返回的 json 字符串

php - FOSRestBundle 无法获取参数 getter

java - ReSTLet 中的 session ?

rest - JAX-RS:验证 JSON 和 XML (RESTEasy)

java - JButtons 在 MouseOver 之前不会出现

json - Google map 地理编码 - 从不返回多个结果

java - 如何解决JSONArray问题,获取不到数据,Android Studio

java - 以相反顺序打印数组,printf 对齐问题