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

时间:2014-09-15 18:40:46

标签: java json

我正在尝试将字符串数组的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();
}

例外是:

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]

1 个答案:

答案 0 :(得分:0)

关闭旧的无效json帖子数据的浏览器缓存。