2013-01-07 19 views
0

Hej guys!谷歌可視化api格式不正確json

我有一個JSON文件,我想從中提取數據以在谷歌條形圖中顯示數據。 Google圖表具有特定的JSON格式,但我的文件中的數據具有不同的JSON格式。 我的問題是,是否有可能將我的JSON文件轉換爲格式,以便它操縱谷歌可視化API的格式?

Google的格式:

{ 
    "cols": [ 
     {"id":"","label":"Topping","pattern":"","type":"string"}, 
     {"id":"","label":"Slices","pattern":"","type":"number"} 
     ], 
    "rows": [ 
     {"c":[{"v":"Mushrooms","f":null},{"v":3,"f":null}]}, 
     {"c":[{"v":"Onions","f":null},{"v":1,"f":null}]}, 
     {"c":[{"v":"Olives","f":null},{"v":1,"f":null}]}, 
     {"c":[{"v":"Zucchini","f":null},{"v":1,"f":null}]}, 
     {"c":[{"v":"Pepperoni","f":null},{"v":2,"f":null}]} 
     ] 
} 

的JSON文件格式:

{ 
    "table.kot.educations": [ 
          {"education":"Math","count":"8"}, 
          {"education":"english","count":"15"}, 
          {"education":"Management","count":"27"}, 
          {"education":"Science","count":"14"}, 
          {"education":"Social studies","count":"15"}, 
          {"education":"Physics","count":"59"}, 
          {"education":"Chemestry","count":"7"} 
          ], 
    "table.user.educations":[ 
          {"education":"Test i afdelingen med et meget meget meget meget langt navn","count":"2"}, 
          {"education":"Test i IT-afdelingen","count":"3"}, 
          {"education":"Test i PR-afdelingen","count":"2"} 
          ], 
    "table.user.education.answer":[ 
          {"education":"Math","answer":"N","count":"2"}, 
          {"education":"Science","answer":"Y","count":"1"}, 
          {"education":"Chemistry","answer":"N","count":"1"}, 
          {"education":"Physics","answer":"Y","count":"1"} 
            ] 
} 

謝謝:)

回答

0

兩個JSON文件似乎完全地有效的給我。 JSON解析器應該忽略縮進,因爲它僅用於更好的可讀性。例如,當使用* json_encode *在PHP中創建JSON字符串時,您可以提供附加參數「JSON_PRETTY_PRINT」,但它不應該更改數據本身 - 只會使人們更容易閱讀它。 http://php.net/manual/en/function.json-encode.php

如果你想反正轉換,通常解析文件到另一個數據格式(如YAML - 這不依賴於壓痕BTW),並將其轉換回,你可以用相同的壓痕結束。

+0

感謝您的回答。我知道這兩種JSON格式都是有效的。我認爲使用Google可視化API創建的圖形只使用上面提到的JSON格式(Google格式)。我想將我的文件JSON格式轉換爲一個Google API可視化API使用的格式。有沒有簡單的方法從一種JSON格式轉換爲另一種? –