2014-12-03 155 views
0

我開發了一個到我的Wordpress網站的鉤子,我可以從其他應用程序更新信息。我正在使用需要特定JSON數據結構的特定插件。我的Java編程技能都有點生疏了,所以我希望有人能幫助我把下面的列表:將列表轉換爲JSON

+------+-------+-----+-------+ 
| 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 | 
+------+-------+-----+-------+ 

並將其轉換爲以下JSON結構:

{ 
    "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" 
     } 
    } 
} 

任何幫助是極大的讚賞。

編輯

我創建了遵循嵌套地圖,但我需要正確的結構,在指定的格式返回數據

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()); 
+4

在那裏,它是 - 它的轉換! – 2014-12-03 01:16:48

+0

Mayme使用3個鏈接的地圖,然後將它們發送到JSON庫(如GSON或Jackson雲)可以做到這一點。 – 2014-12-03 01:19:17

+1

這是一個格式,你會遇到問題{de,}序列化是誠實的。 – fge 2014-12-03 01:24:51

回答

0

這取決於你實際上意味着一點用作您的輸入。您提供的「列表」實際上是一個文本列表。如果你想從各行提取的實際數字,您可以使用正則表達式JS這樣的:

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

其中s是從你的文字輸入線。如果它是包含實際數據線,row將有length === 4,你可以通過這些來像這樣的功能,將其添加到您的數據:通過反覆調用row(a[0], a[1], a[2], a[3])您表的每一行

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; 
} 

,你應該讓你的對象data結構化你想要的方式。

我希望有更優雅的方式來做到這一點(例如,避免數據作爲一個全局變量),但這應該工作。

+0

謝謝,但我正在嘗試在Java中執行此操作,而不是JavaScript。 – bgeveritt 2014-12-03 02:07:30

0

使用這些依賴 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); 
    }  
} 

這是你想要的。

0

得到它具有以下工作:

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()); 
    }