当名称是正则表达式(日期)时的 JSON 解析

发布于 2024-11-08 14:18:09 字数 4463 浏览 0 评论 0原文

如果主题标题不准确,我很抱歉,我真的不知道如何表达它(没有双关语)。 我必须使用类似于下面的结构来解析 JSON。文件中有几个这样的日期数组:每个数组都以 yyyy-MM-dd 形式的日期命名,但我不知道文件中到底有哪些日期(所以我无法通过以下方式提取数组)它的名字)。我还能怎样得到它? 我使用 json.simple for Java 来解析文件。我不期望答案告诉我如何专门使用该库来做到这一点,但如果有人知道,那将是一个额外的好处:)

"2011-05-15":[
     {
        "promoted_content":null,
        "events":null,
        "query":"AnahiHappyBDay",
        "name":"AnahiHappyBDay"
     },
     {
        "promoted_content":null,
        "events":null,
        "query":"Puerto Ricans",
        "name":"Puerto Ricans"
     },
     {
        "promoted_content":null,
        "events":null,
        "query":"Epic Movie",
        "name":"Epic Movie"
     },
     {
        "promoted_content":null,
        "events":null,
        "query":"Lee Soonkyu",
        "name":"Lee Soonkyu"
     },
     {
        "promoted_content":null,
        "events":null,
        "query":"MC1123",
        "name":"MC1123"
     },
     {
        "promoted_content":null,
        "events":null,
        "query":"Dominique Strauss-Kahn",
        "name":"Dominique Strauss-Kahn"
     },
     {
        "promoted_content":null,
        "events":null,
        "query":"WeLoveNickJonas",
        "name":"WeLoveNickJonas"
     },
     {
        "promoted_content":null,
        "events":null,
        "query":"Ripper Stefan",
        "name":"Ripper Stefan"
     },
     {
        "promoted_content":null,
        "events":null,
        "query":"Luc\u00eda P\u00e9rez",
        "name":"Luc\u00eda P\u00e9rez"
     },
     {
        "promoted_content":null,
        "events":null,
        "query":"DB5K",
        "name":"DB5K"
     },
     {
        "promoted_content":null,
        "events":null,
        "query":"WeSupportYouGomez",
        "name":"WeSupportYouGomez"
     },
     {
        "promoted_content":null,
        "events":null,
        "query":"LSnoAltasHoras",
        "name":"LSnoAltasHoras"
     },
     {
        "promoted_content":null,
        "events":null,
        "query":"Azerbaijan",
        "name":"Azerbaijan"
     },
     {
        "promoted_content":null,
        "events":null,
        "query":"Eurovision",
        "name":"Eurovision"
     },
     {
        "promoted_content":null,
        "events":null,
        "query":"Derek Boogaard",
        "name":"Derek Boogaard"
     },
     {
        "promoted_content":null,
        "events":null,
        "query":"Terry Wogan",
        "name":"Terry Wogan"
     },
     {
        "promoted_content":null,
        "events":null,
        "query":"FMI",
        "name":"FMI"
     },
     {
        "promoted_content":null,
        "events":null,
        "query":"WorldLovesSwift",
        "name":"WorldLovesSwift"
     },
     {
        "promoted_content":null,
        "events":null,
        "query":"WorldWarIV",
        "name":"WorldWarIV"
     },
     {
        "promoted_content":null,
        "events":null,
        "query":"Jedward",
        "name":"Jedward"
     },
     {
        "promoted_content":null,
        "events":null,
        "query":"GAGA10MILLION",
        "name":"GAGA10MILLION"
     },
     {
        "promoted_content":null,
        "events":null,
        "query":"Stefan Raab",
        "name":"Stefan Raab"
     },
     {
        "promoted_content":null,
        "events":null,
        "query":"Manchester United Campe\u00f3n",
        "name":"Manchester United Campe\u00f3n"
     },
     {
        "promoted_content":null,
        "events":null,
        "query":"Derek Mooney",
        "name":"Derek Mooney"
     },
     {
        "promoted_content":null,
        "events":null,
        "query":"Chord Overstreet",
        "name":"Chord Overstreet"
     },
     {
        "promoted_content":null,
        "events":null,
        "query":"IMF",
        "name":"IMF"
     },
     {
        "promoted_content":null,
        "events":null,
        "query":"Joe Buck",
        "name":"Joe Buck"
     },
     {
        "promoted_content":null,
        "events":null,
        "query":"\u0410\u0437\u0435\u0440\u0431\u0430\u0439\u0434\u0436\u0430\u043d",
        "name":"\u0410\u0437\u0435\u0440\u0431\u0430\u0439\u0434\u0436\u0430\u043d"
     },
     {
        "promoted_content":null,
        "events":null,
        "query":"Wango Tango",
        "name":"Wango Tango"
     },
     {
        "promoted_content":null,
        "events":null,
        "query":"AlwaysRihannaNavy",
        "name":"AlwaysRihannaNavy"
     }
  ],

Apologies if the Topic title isn't accurate, I didn't really know how to express it (no pun intended).
I have to parse JSON with an structure similar to that below. There are several of these Date arrays in the file: each array is named with a date in the form yyyy-MM-dd, but I do not know exactly which dates will be in the file (so I can't extract the array by its name). How else could I get it?
I'm using json.simple for Java to parse the files. I don't expect an answer telling me how to do it using that library specifically, but if anybody knows, that would be a bonus :)

"2011-05-15":[
     {
        "promoted_content":null,
        "events":null,
        "query":"AnahiHappyBDay",
        "name":"AnahiHappyBDay"
     },
     {
        "promoted_content":null,
        "events":null,
        "query":"Puerto Ricans",
        "name":"Puerto Ricans"
     },
     {
        "promoted_content":null,
        "events":null,
        "query":"Epic Movie",
        "name":"Epic Movie"
     },
     {
        "promoted_content":null,
        "events":null,
        "query":"Lee Soonkyu",
        "name":"Lee Soonkyu"
     },
     {
        "promoted_content":null,
        "events":null,
        "query":"MC1123",
        "name":"MC1123"
     },
     {
        "promoted_content":null,
        "events":null,
        "query":"Dominique Strauss-Kahn",
        "name":"Dominique Strauss-Kahn"
     },
     {
        "promoted_content":null,
        "events":null,
        "query":"WeLoveNickJonas",
        "name":"WeLoveNickJonas"
     },
     {
        "promoted_content":null,
        "events":null,
        "query":"Ripper Stefan",
        "name":"Ripper Stefan"
     },
     {
        "promoted_content":null,
        "events":null,
        "query":"Luc\u00eda P\u00e9rez",
        "name":"Luc\u00eda P\u00e9rez"
     },
     {
        "promoted_content":null,
        "events":null,
        "query":"DB5K",
        "name":"DB5K"
     },
     {
        "promoted_content":null,
        "events":null,
        "query":"WeSupportYouGomez",
        "name":"WeSupportYouGomez"
     },
     {
        "promoted_content":null,
        "events":null,
        "query":"LSnoAltasHoras",
        "name":"LSnoAltasHoras"
     },
     {
        "promoted_content":null,
        "events":null,
        "query":"Azerbaijan",
        "name":"Azerbaijan"
     },
     {
        "promoted_content":null,
        "events":null,
        "query":"Eurovision",
        "name":"Eurovision"
     },
     {
        "promoted_content":null,
        "events":null,
        "query":"Derek Boogaard",
        "name":"Derek Boogaard"
     },
     {
        "promoted_content":null,
        "events":null,
        "query":"Terry Wogan",
        "name":"Terry Wogan"
     },
     {
        "promoted_content":null,
        "events":null,
        "query":"FMI",
        "name":"FMI"
     },
     {
        "promoted_content":null,
        "events":null,
        "query":"WorldLovesSwift",
        "name":"WorldLovesSwift"
     },
     {
        "promoted_content":null,
        "events":null,
        "query":"WorldWarIV",
        "name":"WorldWarIV"
     },
     {
        "promoted_content":null,
        "events":null,
        "query":"Jedward",
        "name":"Jedward"
     },
     {
        "promoted_content":null,
        "events":null,
        "query":"GAGA10MILLION",
        "name":"GAGA10MILLION"
     },
     {
        "promoted_content":null,
        "events":null,
        "query":"Stefan Raab",
        "name":"Stefan Raab"
     },
     {
        "promoted_content":null,
        "events":null,
        "query":"Manchester United Campe\u00f3n",
        "name":"Manchester United Campe\u00f3n"
     },
     {
        "promoted_content":null,
        "events":null,
        "query":"Derek Mooney",
        "name":"Derek Mooney"
     },
     {
        "promoted_content":null,
        "events":null,
        "query":"Chord Overstreet",
        "name":"Chord Overstreet"
     },
     {
        "promoted_content":null,
        "events":null,
        "query":"IMF",
        "name":"IMF"
     },
     {
        "promoted_content":null,
        "events":null,
        "query":"Joe Buck",
        "name":"Joe Buck"
     },
     {
        "promoted_content":null,
        "events":null,
        "query":"\u0410\u0437\u0435\u0440\u0431\u0430\u0439\u0434\u0436\u0430\u043d",
        "name":"\u0410\u0437\u0435\u0440\u0431\u0430\u0439\u0434\u0436\u0430\u043d"
     },
     {
        "promoted_content":null,
        "events":null,
        "query":"Wango Tango",
        "name":"Wango Tango"
     },
     {
        "promoted_content":null,
        "events":null,
        "query":"AlwaysRihannaNavy",
        "name":"AlwaysRihannaNavy"
     }
  ],

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

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

发布评论

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

评论(2

烟花肆意 2024-11-15 14:18:09

我不太确定我是否理解你的问题,但我假设你想要一个日期键列表。

使用 json.simple 解析它应该会产生一个 JSONObject,它是一个映射,因此它的 keySet() 方法将为您提供所需的内容:

JSONObject<String, Object> obj = (JSONObject<String, Object>)JSONValue.parse(s);
for (String key in obj.keySet()) {
  // do something with key
}

I'm not quite sure, if I understand your question, but I assume you want a list of the date keys.

Parsering this with json.simple should result in a JSONObject which is an map, so it's keySet() method will give you what you need:

JSONObject<String, Object> obj = (JSONObject<String, Object>)JSONValue.parse(s);
for (String key in obj.keySet()) {
  // do something with key
}
匿名的好友 2024-11-15 14:18:09

其实我不太明白你的意思...
但是,我想我有一个解决方案[使用 GSON(来自 Google 的库)]
看完你的数据后,我想我们可以创建一个类。像这样

public class Foo{
  private Date date/* or ID ?*/;
  private List<Content> contents; //a class defined blow
  /* constructors and methods */
}

public class Content{
  private String promoted_content;// Or a class? I'm not sure, because I found all of them is null
  private String events;// same as promoted_content
  private String query;
  private String name;    
  /* constructors and methods */
}

现在,使用 gson 解析您的数据。像这样

Gson gson = new Gson();//maybe you wanna read docs of gson for more detail
Foo foo = gson.fromjson(/*your data*/, Foo.class);

在这里你知道你有一个名为 Foo 的对象并且你可以获取它的值。我注意到你说也许有多个“日期”,所以我们可以创建一个包含列表的新类。

Actually I am not very understood what your mean…
However, I thinking I have a solution[Using GSON(a lib from Google)]
After watch your data, I think we can create a Class. like this

public class Foo{
  private Date date/* or ID ?*/;
  private List<Content> contents; //a class defined blow
  /* constructors and methods */
}

public class Content{
  private String promoted_content;// Or a class? I'm not sure, because I found all of them is null
  private String events;// same as promoted_content
  private String query;
  private String name;    
  /* constructors and methods */
}

Now, using gson parse your data. like this

Gson gson = new Gson();//maybe you wanna read docs of gson for more detail
Foo foo = gson.fromjson(/*your data*/, Foo.class);

Here you know you got a object named Foo and you can get its values. I noticed that you said perhaps there are more than ONE "date" so we can create a new class contains List.

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