gpt4 book ai didi

java - 将字符串数组的 json 对象映射到 java 类

转载 作者:太空宇宙 更新时间:2023-11-04 14:33:31 25 4
gpt4 key购买 nike

我正在尝试将字符串数组的 json 对象映射到 java 类。

json:

{ "SKU": ["A", "B", "C"], "Desc": ["Milk", "Bread", "Jam"] }

将 json 映射到的 java 类:

public class Inventory_Metrics {
public String[] SKU;
public String[] Desc;
}

json映射方法:

@Path("/post")
@POST
@Consumes({MediaType.APPLICATION_FORM_URLENCODED, MediaType.APPLICATION_JSON})
@Produces(MediaType.APPLICATION_JSON)
public Response addInventory(String json) throws JsonParseException, JsonMappingException, IOException {
ObjectMapper mapper = new ObjectMapper();
Inventory_Metrics inv_met = mapper.readValue(json, Inventory_Metrics.class);
System.out.print(inv_met.SKU[0]);
return Response.ok().build();
}

异常(exception)情况是:

SEVERE: Servlet.service() for servlet [Jersey REST Service] in context with path [/api] threw exception [org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of com.mycode.database.mapping.Inventory_Metrics out of START_ARRAY token
at [Source: java.io.StringReader@182856b6; line: 1, column: 1]] with root cause
org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of com.mycode.database.mapping.Inventory_Metrics out of START_ARRAY token
at [Source: java.io.StringReader@182856b6; line: 1, column: 1]

最佳答案

关闭旧的无效 json 发布数据的浏览器缓存。

关于java - 将字符串数组的 json 对象映射到 java 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25854691/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com