谷歌可视化api没有正确格式化json

时间:2013-01-07 21:00:32

标签: json google-visualization

Hej家伙!

我有一个JSON文件,我希望从谷歌条形图中提取数据来对数据进行可视化。 Google图表具有特定的JSON格式,但我的文件中的数据具有不同的JSON格式。 我的问题是,是否可以将我的JSON文件转换为格式,以便它操纵谷歌可视化API的格式?

谷歌格式:

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

谢谢:)

1 个答案:

答案 0 :(得分:0)

两个JSON文件对我来说似乎完全有效。 JSON解析器应该忽略缩进,因为它仅用于提高可读性。

例如,当使用* json_encode *在PHP中创建JSON字符串时,您可以提供附加参数“JSON_PRETTY_PRINT”,但它不应该更改数据本身 - 只是让人们更容易阅读它。 http://php.net/manual/en/function.json-encode.php

如果你想要转换它,通常将文件解析为另一种数据格式(如YAML - 它依赖于缩进btw)并将其转换回来,你可能会得到相同的缩进。