0

I've developed a hook into my Wordpress site, where I can update information from another application. I'm using a specific plugin that requires a certain JSON data structure. My Java programming skills are a little rusty, so I was hoping someone could help me take the following List:

+------+-------+-----+-------+
| Year | Month | Day | Value |
+------+-------+-----+-------+
| 2014 |    12 |  22 |     1 |
| 2014 |    12 |  23 |     1 |
| 2014 |    12 |  24 |     1 |
| 2014 |    12 |  25 |     1 |
| 2014 |    12 |  26 |     1 |
| 2015 |     1 |   5 |     1 |
| 2015 |     1 |   6 |     1 |
| 2015 |     1 |   7 |     1 |
| 2015 |     1 |   8 |     1 |
| 2015 |     1 |   9 |     1 |
| 2015 |     1 |  19 |     1 |
| 2015 |     1 |  20 |     1 |
| 2015 |     1 |  21 |     1 |
| 2015 |     1 |  22 |     1 |
| 2015 |     1 |  23 |     1 |
| 2015 |     2 |   2 |     1 |
| 2015 |     2 |   3 |     1 |
| 2015 |     2 |   4 |     1 |
| 2015 |     2 |   5 |     1 |
| 2015 |     2 |   6 |     1 |
+------+-------+-----+-------+

and convert it to the following JSON structure:

{
    "2014": {
        "12": {
            "22": "1",
            "23": "1",
            "24": "1",
            "25": "1",
            "26": "1"
        }
    },
    "2015": {
        "1": {
            "5": "1",
            "6": "1",
            "7": "1",
            "8": "1",
            "9": "1",
            "19": "1",
            "20": "1",
            "21": "1",
            "22": "1",
            "23": "1"
        },
        "2": {
            "2": "1",
            "3": "1",
            "4": "1",
            "5": "1",
            "6": "1"
        }
    }
}

Any help is greatly appreciated.

EDIT

I've created the follow nested Maps, but I need the correct structure to return the data in the specified format

Map<String, Map<String, Map<String, String>>> map = new HashMap<>();
map.put("2014", new HashMap(){{put("12",new HashMap(){{put("22","1");}});}});
map.put("2014", new HashMap(){{put("12",new HashMap(){{put("23","1");}});}});
map.put("2014", new HashMap(){{put("12",new HashMap(){{put("24","1");}});}});
map.put("2014", new HashMap(){{put("12",new HashMap(){{put("25","1");}});}});
map.put("2014", new HashMap(){{put("12",new HashMap(){{put("26","1");}});}});


JSONObject json = new JSONObject(map);
System.out.print(json.toString());
bgeveritt
  • 303
  • 5
  • 18
  • 4
    There it is -- it's converted! – Hot Licks Dec 03 '14 at 01:16
  • Mayme using 3 chained Maps and then sending them to a JSON library such as GSON or Jackson cloud do the trick. – Gilberto Torrezan Dec 03 '14 at 01:19
  • 1
    That's a format which you'll have problems {de,}serializing to be honest. – fge Dec 03 '14 at 01:24
  • What's the data format of the input that your code sees? Is is that ascii output, a database result set, just some Java objects in memory, something else? – Corbell Dec 03 '14 at 01:34
  • The input format will be a List, comprised of those fields. – bgeveritt Dec 03 '14 at 01:41
  • A library might be helpful, after you process the data a bit first: https://code.google.com/p/json-simple/ or https://code.google.com/p/google-gson/. – jpmc26 Dec 03 '14 at 02:21
  • Create the obvious nest of Maps, then serialize with GSON or some such. It's done faster than you can talk about it. – Hot Licks Dec 03 '14 at 02:37
  • Can you take a look at my nested Maps? This is the incorrect structure, but I'm not sure what the proper one is. Question has been updated. – bgeveritt Dec 03 '14 at 03:45

3 Answers3

0

It depends a bit on what you actually mean to use as your input. Your "List" as you've provided it is really a table of text. If you want to extract the actual numbers from each row, you could use a JS regex like this:

row = s.match(/\d+/g)

where s is a line from your text input. If it's a line containing your actual data, row will have length === 4 and you can pass those to a function like this to add it to your data:

var data = {};

function addDay(y, m, d, v) {
    if (!data.hasOwnProperty(y)) {
        data[y] = {};
    }
    if (!data[y].hasOwnProperty(m)) {
        data[y][m] = {};
    }   
    data[y][m][d] = v;
}   

by repeatedly calling row(a[0], a[1], a[2], a[3]) for each row of your table, you should get your object data structured the way you want.

I expect there are more elegant ways to do this (for example, avoiding data as a global variable), but this should work.

OldGeeksGuide
  • 2,888
  • 13
  • 23
0

use these dependencies jackson-databind
jackson-annotations
jackson-core

public class JsonTest {
    public static void main(String[] args) throws JsonProcessingException {
    ObjectMapper mapper=new ObjectMapper();
    Map<String,String> dt=new Hashtable();
    dt.put("1", "welcome");
    dt.put("2", "bye");
    String jsonString = mapper.writeValueAsString(dt)
    System.out.println(jsonString);
    }    
}

This is what you want.

Kumar
  • 3,782
  • 4
  • 39
  • 87
0

Got it working with the following:

    HashMap<String, HashMap<String, HashMap<String, String>>> map = new HashMap<String, HashMap<String, HashMap<String, String>>>();

    for (int i = 0; i < calendarData.size(); i++) {         

        if (!map.containsKey(calendarData.get(i).getYear())) {
            map.put(calendarData.get(i).getYear(), new HashMap<String, HashMap<String, String>>());
        }

        if (!map.get(calendarData.get(i).getYear()).containsKey(calendarData.get(i).getMonth())) {
            map.get(calendarData.get(i).getYear()).put(calendarData.get(i).getMonth(), new HashMap<String, String>());
        }

        map.get(calendarData.get(i).getYear()).get(calendarData.get(i).getMonth()).put(calendarData.get(i).getDay(), calendarData.get(i).getValue());
    }
bgeveritt
  • 303
  • 5
  • 18