球衣号码:JSON数组1元素序列化为对象数组、球衣、序列、元素

2023-09-10 13:24:50 作者:国家一级放水运动员

我要创建一个REST服务器新泽西/ Java和我发现了一个奇怪的现象。

我有一个服务器上的方法,该方法返回一个对象数组为JSON

  @GET
@Path(/文件)
@Produces(MediaType.APPLICATION_JSON)
公共对象的GetFiles()抛出异常{
    的DatabaseManager DB =新的DatabaseManager();
    的FileInfo []结果= db.getFiles();
    返回结果;
}
 

在code正确执行和数据返回给客户端(jQuery的AJAX调用)。 的问题是,返回的数据的变化的格式,如果结果数组有一个元件或多于一个。

响应与一个元素:

  {的fileInfo:{文件名:weather.arff,ID:10}}
 

响应包含两个元素:

{"fileInfo":[{"fileName":"weather.arff","id":"10"},{"fileName":"supermarket.arff","id":"11"}]} GO 指针数组 数组指针 JSON的序列化和反序列化

正如可以看到,在第一个方案中返回的对象的的fileInfo属性的值是一个对象,并且在第二情况下,值是一个数组。 我究竟做错了什么?如果不是第一种情况下返回是这样的:

  {的fileInfo:[{文件名:weather.arff,ID:10}]}
 

即。里面一个对象的数组?

我知道我可以检测到这种在客户端,但它似乎是一个非常丑陋的黑客攻击。

感谢您的时间。

解决方案

我结束了使用杰克逊的官方球衣文档中也描述(http://jersey.java.net/nonav/documentation/latest/user-guide的.html#json.pojo.approach.section)。

我曾尝试之前,但它是行不通的,因为我没有足够的杰克逊瓶子在我的项目的构建路径(基于文档我以为是内置到球衣的核心库)。

我刚刚添加的杰克逊all.jar在文件(http://wiki.fasterxml.com/JacksonDownload)并已在配置中的POJO支持

 <初始化参数>
          <参数 - 名称>&com.sun.jersey.api.json.POJOMappingFeature LT; /参数 - 名称>
          <参数值>真< /参数值>
    < /初始化参数>
 

瞧!

I'm creating a REST server with Jersey/Java and I found a strange behavior.

I have a method on the server that returns an array of objects as Json

@GET
@Path("/files")
@Produces(MediaType.APPLICATION_JSON)
public Object getFiles() throws Exception{
    DatabaseManager db = new DatabaseManager();
    FileInfo[] result = db.getFiles();
    return result;
}

The code is executed correctly and data is returned to the client (a jQuery ajax call). The problem is that the format of the returned data changes if the "result" array has one element or more than one.

Response with one element:

{"fileInfo":{"fileName":"weather.arff","id":"10"}}

Response with two elements:

{"fileInfo":[{"fileName":"weather.arff","id":"10"},{"fileName":"supermarket.arff","id":"11"}]}

As you can see, in the first scenario the value of the "fileInfo" property of the returned object is an object, and in the second case the value is an array. What am I doing wrong? Shouldn't the first case return something like this:

{"fileInfo":[{"fileName":"weather.arff","id":"10"}]}

i.e. an array with a single object inside?

I know that I can detect this on the client side, but it seems like a very ugly hack.

Thanks for your time.

解决方案

I ended up using Jackson, also described in the official Jersey documentation (http://jersey.java.net/nonav/documentation/latest/user-guide.html#json.pojo.approach.section).

I had tried that before but it wasn't working because I didn't have the jackson jar in the buildpath of my project (Based on the documentation I thought it was built into jersey's core library).

I just added the jackson-all.jar file (http://wiki.fasterxml.com/JacksonDownload) and enabled the POJO support in the configuration

    <init-param>
          <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
          <param-value>true</param-value>
    </init-param>

And voilá!