当名称是正则表达式(日期)时的 JSON 解析
如果主题标题不准确,我很抱歉,我真的不知道如何表达它(没有双关语)。 我必须使用类似于下面的结构来解析 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不太确定我是否理解你的问题,但我假设你想要一个日期键列表。
使用 json.simple 解析它应该会产生一个 JSONObject,它是一个映射,因此它的
keySet()
方法将为您提供所需的内容: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:其实我不太明白你的意思...
但是,我想我有一个解决方案[使用 GSON(来自 Google 的库)]
看完你的数据后,我想我们可以创建一个类。像这样
现在,使用 gson 解析您的数据。像这样
在这里你知道你有一个名为 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
Now, using gson parse your data. like this
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.