使用 Gson 解析 NYTimes Search API:如何反序列化嵌套的 JSON 元素?

发布于 2024-09-30 13:48:02 字数 744 浏览 1 评论 0原文

我想解析以 JSON 格式给出的 NYT Search API 的响应。 JSON 字符串如下所示(摘录):

{"facets" : 
  {"des_facet" : 
    [
      {"count" : 745 , "term" : "POLITICS AND GOVERNMENT"} , 
      {"count" : 702 , "term" : "UNITED STATES INTERNATIONAL RELATIONS"}
    ],
   "desk_facet" : 
    [
      {"count" : 2251 , "term" : "Foreign Desk"} , 
      {"count" : 242 , "term" : "Editorial Desk"}
    ]
  }
}

在 Java 方面,我准备了以下对象层次结构:

public class Container {
  Facet facets;
}

public class Facet {
  Collection<Elements> des_facet;
  Collection<Elements> desk_facet;
}

public class Elements {
  private int count;
  private String term;
}

...这显然不起作用。我是 JSON 新手。因此,元素的嵌套结构很混乱。

感谢您的帮助!

I would like to parse a response from the NYT Search API given in JSON format. The JSON string looks as follows (excerpt):

{"facets" : 
  {"des_facet" : 
    [
      {"count" : 745 , "term" : "POLITICS AND GOVERNMENT"} , 
      {"count" : 702 , "term" : "UNITED STATES INTERNATIONAL RELATIONS"}
    ],
   "desk_facet" : 
    [
      {"count" : 2251 , "term" : "Foreign Desk"} , 
      {"count" : 242 , "term" : "Editorial Desk"}
    ]
  }
}

On Java side, i prepared the following object hierarchy:

public class Container {
  Facet facets;
}

public class Facet {
  Collection<Elements> des_facet;
  Collection<Elements> desk_facet;
}

public class Elements {
  private int count;
  private String term;
}

... which is obviously not working. I am new to JSON. Therefore, the nested structure of elements is confusing.

Thanks for your help!

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

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

发布评论

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

评论(1

两仪 2024-10-07 13:48:02

定义的类结构与示例 JSON 完美匹配,并且反序列化对我来说没有错误。

// output: 
// {Container: 
//   facets=
//   {Facet: 
//     des_facet=[
//       {Elements: count=745, term=POLITICS AND GOVERNMENT}, 
//       {Elements: count=702, term=UNITED STATES INTERNATIONAL RELATIONS}
//     ], 
//     desk_facet=[
//       {Elements: count=2251, term=Foreign Desk}, 
//       {Elements: count=242, term=Editorial Desk}
//     ]
//   }
// }

import java.io.FileReader;
import java.util.Collection;

import com.google.gson.Gson;

public class Foo
{
  public static void main(String[] args) throws Exception
  {
    Gson gson = new Gson();
    Container container = gson.fromJson(new FileReader("input.json"), Container.class);
    System.out.println(container);
  }
}

class Container
{
  Facet facets;

  @Override
  public String toString()
  {
    return String.format("{Container: facets=%s}", facets);
  }
}

class Facet
{
  Collection<Elements> des_facet;
  Collection<Elements> desk_facet;

  @Override
  public String toString()
  {
    return String.format("{Facet: des_facet=%s, desk_facet=%s}", des_facet, desk_facet);
  }
}

class Elements
{
  private int count;
  private String term;

  @Override
  public String toString()
  {
    return String.format("{Elements: count=%d, term=%s}", count, term);
  }
}

The defined class structure matches the example JSON perfectly well, and deserializes without error for me.

// output: 
// {Container: 
//   facets=
//   {Facet: 
//     des_facet=[
//       {Elements: count=745, term=POLITICS AND GOVERNMENT}, 
//       {Elements: count=702, term=UNITED STATES INTERNATIONAL RELATIONS}
//     ], 
//     desk_facet=[
//       {Elements: count=2251, term=Foreign Desk}, 
//       {Elements: count=242, term=Editorial Desk}
//     ]
//   }
// }

import java.io.FileReader;
import java.util.Collection;

import com.google.gson.Gson;

public class Foo
{
  public static void main(String[] args) throws Exception
  {
    Gson gson = new Gson();
    Container container = gson.fromJson(new FileReader("input.json"), Container.class);
    System.out.println(container);
  }
}

class Container
{
  Facet facets;

  @Override
  public String toString()
  {
    return String.format("{Container: facets=%s}", facets);
  }
}

class Facet
{
  Collection<Elements> des_facet;
  Collection<Elements> desk_facet;

  @Override
  public String toString()
  {
    return String.format("{Facet: des_facet=%s, desk_facet=%s}", des_facet, desk_facet);
  }
}

class Elements
{
  private int count;
  private String term;

  @Override
  public String toString()
  {
    return String.format("{Elements: count=%d, term=%s}", count, term);
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文