简体   繁体   English

在Jersey中解析复杂的JSON类型

[英]Parsing a complex JSON type in Jersey

I'm using Jersey to create a servlet that takes a slightly complex JSON structure: 我正在使用Jersey创建一个采用稍微复杂的JSON结构的servlet:

[
  {
    "name": "bob",
    "events": [
      {
        "type": "a",
        "value": "b"
      },
      {
        "type": "x",
        "value": "y"
      }
    ]
  },
  {
    "name": "alice",
    "events": [
    {
      "type": "one",
      "value": "two"
    },
    {
      "type": "three",
      "value": "four"
    }
    ]
  }
]

I have the following data classes: 我有以下数据类:

public class Read
{
  private String name;
  private ArrayList<Event> events;
  // Getters & Setters
}

public class Event
{
  private String type;
  private String value;
  // Getters & Setters
}

I want to be able to take in JSON and put it into an ArrayList<Read> object. 我希望能够接受JSON并将其放入ArrayList<Read>对象。 I have the following code: 我有以下代码:

@Path("/postreads")
@Consumes(MediaType.APPLICATION_JSON)
public class PostReads
{
  @POST
  @Path("/batch")
  public Response postReads(ArrayList<Read> reads)
  {
    for (Read read : reads)
    {
      System.out.println(read.toString());
    }

    return Response.status(200).entity("Success.").build();    
  }
}

When I had it doing a simple object (the Event , with just two String properties), it worked fine. 当我使用一个简单的对象(带有两个String属性的Event )时,它可以正常工作。 But adding the complexity of the ArrayList s killed it. 但是增加ArrayList的复杂性可以杀死它。 Is there a way to automagically parse that JSON, or do I need to do it manually? 有没有办法自动解析该JSON,还是我需要手动进行? It's a pretty open project at this point, so if I need to add an extra library, I can. 此时,这是一个非常开放的项目,因此,如果我需要添加一个额外的库,可以。

The error I'm getting on the eclipse console: 我在Eclipse控制台上遇到的错误:

Jun 03, 2014 2:44:01 PM com.sun.jersey.spi.container.ContainerRequest getEntity
SEVERE: A message body reader for Java class java.util.ArrayList, and Java type java.util.ArrayList<com.project.postevents.Event>, and MIME media type application/json was not found.
The registered message body readers compatible with the MIME media type are:
application/json ->
  com.sun.jersey.json.impl.provider.entity.JSONJAXBElementProvider$App
  com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider$App
  com.sun.jersey.json.impl.provider.entity.JSONListElementProvider$App
*/* ->
  com.sun.jersey.core.impl.provider.entity.FormProvider
  com.sun.jersey.core.impl.provider.entity.StringProvider
  com.sun.jersey.core.impl.provider.entity.ByteArrayProvider
  com.sun.jersey.core.impl.provider.entity.FileProvider
  com.sun.jersey.core.impl.provider.entity.InputStreamProvider
  com.sun.jersey.core.impl.provider.entity.DataSourceProvider
  com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General
  com.sun.jersey.core.impl.provider.entity.ReaderProvider
  com.sun.jersey.core.impl.provider.entity.DocumentProvider
  com.sun.jersey.core.impl.provider.entity.SourceProvider$StreamSourceReader
  com.sun.jersey.core.impl.provider.entity.SourceProvider$SAXSourceReader
  com.sun.jersey.core.impl.provider.entity.SourceProvider$DOMSourceReader
  com.sun.jersey.json.impl.provider.entity.JSONJAXBElementProvider$General
  com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General
  com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General
  com.sun.jersey.core.impl.provider.entity.XMLRootObjectProvider$General
  com.sun.jersey.core.impl.provider.entity.EntityHolderReader
  com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider$General
  com.sun.jersey.json.impl.provider.entity.JSONListElementProvider$General

Edit: The comma after the square bracket was a typo. 编辑:方括号后的逗号是一个错字。 The incoming JSON validates. 传入的JSON验证。

Consider using google-gson : 考虑使用google-gson

Gson is a Java library that can be used to convert Java Objects into their JSON representation. Gson是一个Java库,可用于将Java对象转换为其JSON表示形式。 It can also be used to convert a JSON string to an equivalent Java object. 它还可以用于将JSON字符串转换为等效的Java对象。 Gson can work with arbitrary Java objects including pre-existing objects that you do not have source-code of. Gson可以处理任意Java对象,包括您没有源代码的现有对象。

Sample code for your use case (via the user guide ): 您的用例的示例代码(通过用户指南 ):

class BagOfPrimitives {
  private int value1 = 1;
  private String value2 = "abc";
  private transient int value3 = 3;
  BagOfPrimitives() {
    // no-args constructor
  }
}

// Deserialization
Gson gson = new Gson();
String json = ...;
BagOfPrimitives obj2 = gson.fromJson(json, BagOfPrimitives.class);

There are more complex examples with arrays, collections, etc. on that page as well. 该页面上也有更复杂的示例,其中包含数组,集合等。

Disclosure: I work at Google, but am not affiliated with google-gson. 披露:我在Google工作,但不隶属于google-gson。

The error you are having is because your JSON file isn't valid. 您遇到的错误是因为JSON文件无效。 the coma's after the square brackets should not be there. 方括号后的昏迷不应该出现。

Error: Parse error on line 13:
...      }
    ],
 },
{
    "name":
 --------------------^
Expecting 'STRING', got '}'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM