0

我的 .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 库,所以如果这个库中包含一些我可以使用的功能,请提及它们。

4

2 回答 2

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;
于 2013-03-18T16:41:07.597 回答
-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; 
于 2013-03-18T16:37:57.160 回答