2015-07-13 59 views
0

下面是一個Ajax調用的responseText的一個元素:如何訪問使用javascript的對象數組中的對象的屬性?

[{ 
    "hitcount": { 
     "0": "3351" 
    }, 
    "page": { 
     "0": "1" 
    }, 
    "title": { 
     "0": "Hypoxia upregulates Rab11-family interacting protein 4 through HIF-1\u03b1 to promote the metastasis of hepatocellular carcinoma." 
    }, 
    "authorString": { 
     "0": "Hu F, Deng X, Yang X, Jin H, Gu D, Lv X, Wang C, Zhang Y, Huo X, Shen Q, Luo Q, Zhao F, Ge T, Zhao F, Chu W, Shu H, Yao M, Fan J, Qin W." 
    }, 
    "pubYear": { 
     "0": "2015" 
    }, 
    "journalVolume": "", 
    "issue": "", 
    "pageInfo": "", 
    "pmid": { 
     "0": "25745995" 
    }, 
    "abstract_": { 
     "0": "Hypoxic microenvironment is a powerful driving force for the invasion and metastasis of hepatocellular carcinoma (HCC). Hypoxia-inducible factor 1\u03b1 (HIF-1\u03b1), as a crucial regulator of transcriptional responses to hypoxia, induces the expression of multiple target genes involved in different steps of HCC metastatic process. It is critical to find target genes associated with metastasis under hypoxia for shedding new light on molecular mechanism of HCC metastasis. In this study, we uncovered that hypoxia could induce the upregulation of Rab11-family interacting protein 4 (Rab11-FIP4) and activation of Rab11-FIP4 promoter by HIF-1\u03b1. The overexpression of Rab11-FIP4 significantly enhanced the mobility and invasiveness of HCC cells in vitro, also contributed to distant lung metastasis in vivo, whereas silencing of Rab11-FIP4 decreased the ability of migration and invasion in HCC cells in vitro and suppressed lung metastasis in vivo. Rab11-FIP4 facilitated HCC metastasis through the phosphorylation of PRAS40, which was regulated by mTOR. Furthermore, the expression level of Rab11-FIP4 was significantly increased in HCC tissues and high expression of Rab11-FIP4 was closely correlated with vascular invasion and poor prognosis in HCC patients. A markedly positive correlation between the expression of Rab11-FIP4 and HIF-1\u03b1 was observed in HCC tissues and combination of Rab11-FIP4 and HIF-1\u03b1 was a more valuable predictor of poor prognosis for HCC patients. In conclusion, Rab11-FIP4 is a target gene of HIF-1\u03b1 and has a pro-metastatic role in HCC, suggesting that Rab11-FIP4 may be a promising candidate target for HCC treatment.Oncogene advance online publication, 9 March 2015; doi:10.1038\/onc.2015.49." 
    }, 
    "journalTitle": { 
     "0": "Oncogene" 
    }, 
    "pdfURL": "", 
    "arrayOfPersons": [{ 
     "firstname": "dean", 
     "lastname": "wood", 
     "avatar_path": null, 
     "username": "deanwood" 
    }, { 
     "firstname": "dean", 
     "lastname": "wood", 
     "avatar_path": null, 
     "username": "deanwood" 
    }, { 
     "firstname": "Anne", 
     "lastname": "Smith", 
     "avatar_path": null, 
     "username": "annesmith" 
    }] 
}] 

我的字符串轉換爲JavaScript對象:

var json_obj = JSON.parse(xmlhttp.responseText); 

我試圖訪問對象數組的屬性從這個元素(位於元素的末尾),但沒有成功。我試過以下沒有成功:

json_obj[i].arrayOfPersons[i].firstname; 

請問可以somone提醒嗎?

+1

這是無效的JSON的開始。你確定這正是XHR出現的內容嗎? –

+0

在兩個位置使用相同的「i」是沒有意義的。一般來說,你看起來不錯,你只需確保你正在訪問正確的元素。 –

+0

你用'json_obj [0] .arrayOfPersons [i] .firstname;'試過了嗎? – Timo

回答

0

獲取arrayOfPersons的最後一個人:

var json = '[{"hitcount":{"0":"3351"},"page":{"0":"1"},"title":{"0":"Hypoxia upregulates Rab11-family interacting protein 4 through HIF-1\u03b1 to promote the metastasis of hepatocellular carcinoma."},"authorString":{"0":"Hu F, Deng X, Yang X, Jin H, Gu D, Lv X, Wang C, Zhang Y, Huo X, Shen Q, Luo Q, Zhao F, Ge T, Zhao F, Chu W, Shu H, Yao M, Fan J, Qin W."},"pubYear":{"0":"2015"},"journalVolume":"","issue":"","pageInfo":"","pmid":{"0":"25745995"},"abstract_":{"0":"Hypoxic microenvironment is a powerful driving force for the invasion and metastasis of hepatocellular carcinoma (HCC). Hypoxia-inducible factor 1\u03b1 (HIF-1\u03b1), as a crucial regulator of transcriptional responses to hypoxia, induces the expression of multiple target genes involved in different steps of HCC metastatic process. It is critical to find target genes associated with metastasis under hypoxia for shedding new light on molecular mechanism of HCC metastasis. In this study, we uncovered that hypoxia could induce the upregulation of Rab11-family interacting protein 4 (Rab11-FIP4) and activation of Rab11-FIP4 promoter by HIF-1\u03b1. The overexpression of Rab11-FIP4 significantly enhanced the mobility and invasiveness of HCC cells in vitro, also contributed to distant lung metastasis in vivo, whereas silencing of Rab11-FIP4 decreased the ability of migration and invasion in HCC cells in vitro and suppressed lung metastasis in vivo. Rab11-FIP4 facilitated HCC metastasis through the phosphorylation of PRAS40, which was regulated by mTOR. Furthermore, the expression level of Rab11-FIP4 was significantly increased in HCC tissues and high expression of Rab11-FIP4 was closely correlated with vascular invasion and poor prognosis in HCC patients. A markedly positive correlation between the expression of Rab11-FIP4 and HIF-1\u03b1 was observed in HCC tissues and combination of Rab11-FIP4 and HIF-1\u03b1 was a more valuable predictor of poor prognosis for HCC patients. In conclusion, Rab11-FIP4 is a target gene of HIF-1\u03b1 and has a pro-metastatic role in HCC, suggesting that Rab11-FIP4 may be a promising candidate target for HCC treatment.Oncogene advance online publication, 9 March 2015; doi:10.1038\/onc.2015.49."},"journalTitle":{"0":"Oncogene"},"pdfURL":"","arrayOfPersons":[{"firstname":"dean","lastname":"wood","avatar_path":null,"username":"deanwood"},{"firstname":"dean","lastname":"wood","avatar_path":null,"username":"deanwood"},{"firstname":"Anne","lastname":"Smith","avatar_path":null,"username":"annesmith"}]}]'; 
 

 
function show() { 
 
    var json_obj = JSON.parse(json); 
 
    result = document.getElementById("result"); 
 
    lastPerson = json_obj[0].arrayOfPersons[json_obj[0].arrayOfPersons.length - 1]; 
 
    result.innerHTML = lastPerson.firstname + " " +lastPerson.lastname; 
 
}
Last person of arrayOfPersons: 
 
<div id="result"></div> 
 
<button onclick="show();">Show</button>

0

首先:解決你的JSON,它在最後缺少一個]。 除此之外,你可能想遍歷所有可能的firstNames。 試試這個:

for (var i = 0; i < json_obj.length; i++) { 
    for (var j = 0; j < json_obj[i].arrayOfPersons.length; j++) { 
     var result = json_obj[i].arrayOfPersons[j]; 
     console.log(result.firstname); 
    } 
} 
0

如果xmlhttp.responseText返回值的類型是JSON,你並不需要解析它。你可以這樣使用:

var json_obj = xmlhttp.responseText; 
json_obj[i].arrayOfPersons[i].firstname