我的.json文件是:
{
"links":[
{source: "algorithms cormen", target: "irodov", type: "default"},
{source: "algorithms cormen", target: "skienna", type: "default"},
{source: "irodov", target: "skienna", type: "default"},
{source: "irodov", target: "algorithms cormen", type: "default"}
],
"data":[
{node: "algorithms cormen", data: "bible of algos"},
{node: "irodov", data: "bibile of physics"},
{node: "skienna", data: "cool book"}
]
}
现在在我的javascript文件中,我希望“链接”存储的数据转到链接变量。我想用“数据”做同样的事情 我该如何实现这一目标?我正在使用d3.js库,所以如果我可以使用这个库中包含的某些功能,请提及它们。
答案 0 :(得分:0)
只需解析JSON并将值赋给变量:
var json; // I am assuming this is populated with your JSON string
var jsonObj = JSON.parse(json);
var links = jsonObj.links;
var data = jsonObj.data;
答案 1 :(得分:-1)
将json文件更新为(假设您在页面加载时不包含json文件而不是通过ajax)
var json-data = {
"links":[
{source: "algorithms cormen", target: "irodov", type: "default"},
{source: "algorithms cormen", target: "skienna", type: "default"},
{source: "irodov", target: "skienna", type: "default"},
{source: "irodov", target: "algorithms cormen", type: "default"}
],
"data":[
{node: "algorithms cormen", data: "bible of algos"},
{node: "irodov", data: "bibile of physics"},
{node: "skienna", data: "cool book"}
]
}
然后访问
var links = json-data.links;
var data = json-data.data;