2013-03-18 5 views
0

내 .json 파일은 다음과 같습니다.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"} 
] 
} 

, 나는 "링크"에 의해 저장된 데이터는 링크 변수에 가고 싶어. "데이터"로 수행하고자하는 것과 동일한 작업입니다. 어떻게 이것을합니까? d3.js 라이브러리를 사용하고 있습니다. 따라서이 라이브러리에 포함 된 일부 함수가 있다면 사용할 수 있습니다.

+0

var links = JSON.parse (yourjson) .links'? – andy

답변

-1

업데이트 (당신이하지 아약스를 통해 페이지로드에있는 JSON 파일을 포함하는 가정)에 JSON 파일은

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; 
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; 
관련 문제