0

嘿伙计们!

我有一个 JSON 文件,我想从中提取数据以在谷歌条形图中可视化数据。谷歌图表有特定的 JSON 格式,但我的文件中的数据是不同的 JSON 格式。我的问题是,是否可以将我的 JSON 文件转换为该格式,以便它操纵 google 可视化 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"}
                                  ]
} 

谢谢 :)

4

1 回答 1

0

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

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

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

于 2013-01-07T21:10:42.463 回答