问题:使用 Jersey 序列化,使用 gson 反序列化 JSON

发布于 2024-11-06 15:04:17 字数 1670 浏览 0 评论 0原文

我一直在使用 Jersey 进行 REST API 并返回 JSON。在客户端,我使用 google-gson。反序列化 JSON 时,出现以下错误。

com.google.gson.JsonParseException: The JsonDeserializer MapTypeAdapter failed to deserialized json object {} given the type interface java.util.Map
        at com.google.gson.JsonDeserializerExceptionWrapper.deserialize(JsonDeserializerExceptionWrapper.java:63)
        at com.google.gson.JsonDeserializationVisitor.invokeCustomDeserializer(JsonDeserializationVisitor.java:88)
        at com.google.gson.JsonObjectDeserializationVisitor.visitFieldUsingCustomHandler(JsonObjectDeserializationVisitor.java:116)
        at com.google.gson.ObjectNavigator.navigateClassFields(ObjectNavigator.java:158)
        at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:131)
        at com.google.gson.JsonDeserializationCo

Jersey 序列化为 JSON 的对象具有非通用(未指定类型)映射和列表

public class Dealer implements Serializable
{
  private String serviceURL;
  private Map hoursService;
  List dealerAttributes;  
}

这是 Jersey 生成的 JSON

{"serviceURL":www.google.com,"hoursService":{"3":{"dayOfWeek":3,"closeTime":"6:30 PM","openTime":"7:30 AM"},"2":{"dayOfWeek":2,"closeTime":"6:30 PM","openTime":"7:30 AM"},"1":{"dayOfWeek":1,"closeTime":"6:30 PM","openTime":"7:30 AM"}},"dealerAttributes":[{"language":"English","dealerAttributeName":"Spanish Speaking","updateDate":1174971061000},{"language":"English","updateDate":1103003316000}]}

可能是什么原因。请帮我解决这个问题。还有其他比 gson 更好的实用程序吗?请建议。

谢谢。

I have been using Jersey for REST API and returning JSON. On client side I am using google-gson. While deserializing JSON, I am getting following error.

com.google.gson.JsonParseException: The JsonDeserializer MapTypeAdapter failed to deserialized json object {} given the type interface java.util.Map
        at com.google.gson.JsonDeserializerExceptionWrapper.deserialize(JsonDeserializerExceptionWrapper.java:63)
        at com.google.gson.JsonDeserializationVisitor.invokeCustomDeserializer(JsonDeserializationVisitor.java:88)
        at com.google.gson.JsonObjectDeserializationVisitor.visitFieldUsingCustomHandler(JsonObjectDeserializationVisitor.java:116)
        at com.google.gson.ObjectNavigator.navigateClassFields(ObjectNavigator.java:158)
        at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:131)
        at com.google.gson.JsonDeserializationCo

The Object which is being serialized to JSON by Jersey is having non-generic(no type specified) Map and List

public class Dealer implements Serializable
{
  private String serviceURL;
  private Map hoursService;
  List dealerAttributes;  
}

Here is the JSON generated by Jersey

{"serviceURL":www.google.com,"hoursService":{"3":{"dayOfWeek":3,"closeTime":"6:30 PM","openTime":"7:30 AM"},"2":{"dayOfWeek":2,"closeTime":"6:30 PM","openTime":"7:30 AM"},"1":{"dayOfWeek":1,"closeTime":"6:30 PM","openTime":"7:30 AM"}},"dealerAttributes":[{"language":"English","dealerAttributeName":"Spanish Speaking","updateDate":1174971061000},{"language":"English","updateDate":1103003316000}]}

What could be the reason. Please help me to resolve this issue. Is there any other utility which is better than gson? Please suggest.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

五里雾 2024-11-13 15:04:17

示例 JSON 无效。 http://jsonlint.com

www.google.com 必须用引号引起来。那么它就有效了。

纠正 JSON 后,以下反序列化示例输出

[Dealer: serviceURL=www.google.com, hoursService={
1=[Hours: dayOfWeek=1, closeTime=6:30 PM, openTime=7:30 AM], 
2=[Hours: dayOfWeek=2, closeTime=6:30 PM, openTime=7:30 AM], 
3=[Hours: dayOfWeek=3, closeTime=6:30 PM, openTime=7:30 AM]}, 
dealerAttributes=[
[DealerAttributes: language=English, dealerAttributeName=Spanish Speaking, updateDate=1174971061000], 
[DealerAttributes: language=English, dealerAttributeName=null, updateDate=1103003316000]]]
public class Foo
{
  static String jsonInput = 
    "{" + 
      "\"serviceURL\":\"www.google.com\"," + 
      "\"hoursService\":" +
      "{" + 
        "\"1\":" +
        "{" + 
          "\"dayOfWeek\":1," + 
          "\"closeTime\":\"6:30 PM\"," + 
          "\"openTime\":\"7:30 AM\"" + 
        "}," + 
        "\"2\":" +
        "{" + 
          "\"dayOfWeek\":2," + 
          "\"closeTime\":\"6:30 PM\"," + 
          "\"openTime\":\"7:30 AM\"" + 
        "}," + 
        "\"3\":" +
        "{" + 
          "\"dayOfWeek\":3," + 
          "\"closeTime\":\"6:30 PM\"," + 
          "\"openTime\":\"7:30 AM\"" + 
        "}" + 
      "}," + 
      "\"dealerAttributes\":" +
      "[" + 
        "{" + 
          "\"language\":\"English\"," + 
          "\"dealerAttributeName\":\"Spanish Speaking\"," + 
          "\"updateDate\":1174971061000" + 
        "}," + 
        "{" + 
          "\"language\":\"English\"," + 
          "\"updateDate\":1103003316000" + 
        "}" + 
      "]" + 
    "}";

  public static void main(String[] args)
  {
    GsonBuilder gsonBuilder = new GsonBuilder();
    Gson gson = gsonBuilder.create();
    Dealer dealer = gson.fromJson(jsonInput, Dealer.class);
    System.out.println(dealer);
  }
}

class Dealer
{
  private String serviceURL;
  private Map<String, Hours> hoursService;
  private List<DealerAttributes> dealerAttributes;

  @Override
  public String toString()
  {
    return String.format(
      "[Dealer: serviceURL=%1$s, hoursService=%2$s, dealerAttributes=%3$s]",
      serviceURL, hoursService, dealerAttributes);
  }
}

class Hours
{
  private int dayOfWeek;
  private String closeTime;
  private String openTime;

  @Override
  public String toString()
  {
    return String.format(
      "[Hours: dayOfWeek=%1$d, closeTime=%2$s, openTime=%3$s]",
      dayOfWeek, closeTime, openTime);
  }
}

class DealerAttributes
{
  private String language;
  private String dealerAttributeName;
  private long updateDate;

  @Override
  public String toString()
  {
    return String.format(
      "[DealerAttributes: language=%1$s, dealerAttributeName=%2$s, updateDate=%3$d]",
      language, dealerAttributeName, updateDate);
  }
}

This does use generic types,我理解你说你没有使用它来序列化,但是你在反序列化时是否遇到了相同的限制?如果是这样,那么您将必须实施更多“手动”反序列化解析,因为正如文档所说,"[w]反序列化时,集合必须是特定的泛型类型"

The example JSON is invalid. http://jsonlint.com

www.google.com must be enclosed with quotes. Then it is valid.

With the JSON corrected, the following deserialization example outputs

[Dealer: serviceURL=www.google.com, hoursService={
1=[Hours: dayOfWeek=1, closeTime=6:30 PM, openTime=7:30 AM], 
2=[Hours: dayOfWeek=2, closeTime=6:30 PM, openTime=7:30 AM], 
3=[Hours: dayOfWeek=3, closeTime=6:30 PM, openTime=7:30 AM]}, 
dealerAttributes=[
[DealerAttributes: language=English, dealerAttributeName=Spanish Speaking, updateDate=1174971061000], 
[DealerAttributes: language=English, dealerAttributeName=null, updateDate=1103003316000]]]
public class Foo
{
  static String jsonInput = 
    "{" + 
      "\"serviceURL\":\"www.google.com\"," + 
      "\"hoursService\":" +
      "{" + 
        "\"1\":" +
        "{" + 
          "\"dayOfWeek\":1," + 
          "\"closeTime\":\"6:30 PM\"," + 
          "\"openTime\":\"7:30 AM\"" + 
        "}," + 
        "\"2\":" +
        "{" + 
          "\"dayOfWeek\":2," + 
          "\"closeTime\":\"6:30 PM\"," + 
          "\"openTime\":\"7:30 AM\"" + 
        "}," + 
        "\"3\":" +
        "{" + 
          "\"dayOfWeek\":3," + 
          "\"closeTime\":\"6:30 PM\"," + 
          "\"openTime\":\"7:30 AM\"" + 
        "}" + 
      "}," + 
      "\"dealerAttributes\":" +
      "[" + 
        "{" + 
          "\"language\":\"English\"," + 
          "\"dealerAttributeName\":\"Spanish Speaking\"," + 
          "\"updateDate\":1174971061000" + 
        "}," + 
        "{" + 
          "\"language\":\"English\"," + 
          "\"updateDate\":1103003316000" + 
        "}" + 
      "]" + 
    "}";

  public static void main(String[] args)
  {
    GsonBuilder gsonBuilder = new GsonBuilder();
    Gson gson = gsonBuilder.create();
    Dealer dealer = gson.fromJson(jsonInput, Dealer.class);
    System.out.println(dealer);
  }
}

class Dealer
{
  private String serviceURL;
  private Map<String, Hours> hoursService;
  private List<DealerAttributes> dealerAttributes;

  @Override
  public String toString()
  {
    return String.format(
      "[Dealer: serviceURL=%1$s, hoursService=%2$s, dealerAttributes=%3$s]",
      serviceURL, hoursService, dealerAttributes);
  }
}

class Hours
{
  private int dayOfWeek;
  private String closeTime;
  private String openTime;

  @Override
  public String toString()
  {
    return String.format(
      "[Hours: dayOfWeek=%1$d, closeTime=%2$s, openTime=%3$s]",
      dayOfWeek, closeTime, openTime);
  }
}

class DealerAttributes
{
  private String language;
  private String dealerAttributeName;
  private long updateDate;

  @Override
  public String toString()
  {
    return String.format(
      "[DealerAttributes: language=%1$s, dealerAttributeName=%2$s, updateDate=%3$d]",
      language, dealerAttributeName, updateDate);
  }
}

This does use generic types, which I understand you said you weren't using to serialize, but are you stuck with the same limitation when deserialization? If so, then you'll have to implement some more "manual" deserialization parsing, because, as the docs say, "[w]hile deserializing, Collection must be of a specific generic type".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文