开发者

JSON parsing when names are regexes (dates)

开发者 https://www.devze.com 2023-03-07 13:22 出处:网络
Apologies if the Topic title isn\'t accurate, I didn\'t really know how to express it (no pun intended).

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"
     },
     {开发者_如何学C
        "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"
     }
  ],


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
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号