Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/476.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何访问放置在javascript对象中的对象的属性?_Javascript_Php - Fatal编程技术网

如何访问放置在javascript对象中的对象的属性?

如何访问放置在javascript对象中的对象的属性?,javascript,php,Javascript,Php,我有一个php文件,它将对象的json字符串发送到我的javascript文件。我在访问此javascript对象中包含的对象的属性时遇到问题 我的php文件: for($i=0; $i<$hitcount; $i++){ if($i == $hitcount-1){ // IF HITCOUNT IS LESS THAN 25 RESULTS echo json_encode($articleArray);

我有一个php文件,它将对象的json字符串发送到我的javascript文件。我在访问此javascript对象中包含的对象的属性时遇到问题

我的php文件:

 for($i=0; $i<$hitcount; $i++){
        if($i == $hitcount-1){
            // IF HITCOUNT IS LESS THAN 25 RESULTS
            echo json_encode($articleArray);
            exit();
        }else if($i == 25){
            // THERE IS ONLY A MAX OF 25 RESULTS PER REQUEST
            echo json_encode($articleArray);
            exit();
        }else{
            $page = $xml -> request  -> page ; //always page 1

    $title = $xml ->resultList -> result[$i] -> title;
        if($title == false){
            $title = '';
        }

    $pmid = $xml ->resultList -> result[$i] -> pmid;
        if($pmid == false){
            $pmid = '';
        }

    $authorString = $xml ->resultList -> result[$i] -> authorString;
        if($authorString == false){
            $authorString ='';
        }
    $journalTitle  = $xml ->resultList -> result[$i] -> journalTitle ;
        if($journalTitle ==false){
            $journalTitle = '';
        }
    $pubYear = $xml ->resultList -> result[$i] -> pubYear;
        if($pubYear == false){
            $pubYear = '';
        }
    $journalVolume = $xml ->resultList -> result[$i] -> journalVolume;
        if($journalVolume == false){
            $journalVolume = '';
        }
    $issue = $xml ->resultList -> result[v] -> issue;
        if($issue == false){
            $issue = '';
        }
    $pageInfo = $xml ->resultList -> result[$i] -> pageInfo;
        if($pageInfo == false){
            $pageInfo = '';
        }


        $articleArray[] = array(
            'hitCount' => $hitcount,
            'page' => $page,
            'pmid' => $pmid,
            'title' => $title,
            'authors' => $authorString,
            'journalTitle' => $journalTitle,
            'pubYear' => $pubYear,
            'journalVolume' => $journalVolume,
            'issue' => $issue,
            'pageInfo' => $pageInfo
            );
        }
    }
我尝试了以下方法来访问存储在javascript对象中的对象的属性:

 json_obj[0].title // to access the title of the first object
这只是返回:

 [object Object]
xmlhttp.responseText:

 [{"hitCount":{"0":"3347"},"page":{"0":"1"},"pmid":{"0":"25745995"},"title":{"0":"Hypoxia upregulates Rab11-family interacting protein 4 through HIF-1\u03b1 to promote the metastasis of hepatocellular carcinoma."},"authors":{"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."},"journalTitle":{"0":"Oncogene"},"pubYear":{"0":"2015"},"journalVolume":"","issue":"","pageInfo":""},{"hitCount":{"0":"3347"},"page":{"0":"1"},"pmid":{"0":"25800835"},"title":{"0":"Kinetic activation of Rab8 guanine nucleotide exchange factor Rabin8 by Rab11."},"authors":{"0":"Feng S, Wu B, Per\u00e4nen J, Guo W."},"journalTitle":{"0":"Methods Mol Biol"},"pubYear":{"0":"2015"},"journalVolume":{"0":"1298"},"issue":"","pageInfo":{"0":"99-106"}},{"hitCount":{"0":"3347"},"page":{"0":"1"},"pmid":{"0":"26032412"},"title":{"0":"Structure-Function Analyses of the Interactions Between Rab11 and Rab14 Small GTPases with their Shared Effector Rab Coupling Protein (RCP)."},"authors":{"0":"Lall P, Lindsay A, Hanscom S, Kecman T, Taglauer ES, McVeigh UM, Franklin E, McCaffrey MW, Khan AR."},"journalTitle":{"0":"J Biol Chem"},"pubYear":{"0":"2015"},"journalVolume":"","issue":"","pageInfo":""},{"hitCount":{"0":"3347"},"page":{"0":"1"},"pmid":{"0":"25815277"},"title":{"0":"Rab11 in disease progression."},"authors":{"0":"Bhuin T, Roy JK."},"journalTitle":{"0":"Int J Mol Cell Med"},"pubYear":{"0":"2015"},"journalVolume":{"0":"4"},"issue":"","pageInfo":{"0":"1-8"}},{"hitCount":{"0":"3347"},"page":{"0":"1"},"pmid":{"0":"25800843"},"title":{"0":"In vitro and in vivo characterization of the Rab11-GAP activity of Drosophila Evi5."},"authors":{"0":"Laflamme C, Emery G."},"journalTitle":{"0":"Methods Mol Biol"},"pubYear":{"0":"2015"},"journalVolume":{"0":"1298"},"issue":"","pageInfo":{"0":"187-194"}},{"hitCount":{"0":"3347"},"page":{"0":"1"},"pmid":{"0":"24917558"},"title":{"0":"Vasodilator-stimulated phosphoprotein promotes activation of hepatic stellate cells by regulating Rab11-dependent plasma membrane targeting of transforming growth factor beta receptors."},"authors":{"0":"Tu K, Li J, Verma VK, Liu C, Billadeau DD, Lamprecht G, Xiang X, Guo L, Dhanasekaran R, Roberts LR, Shah VH, Kang N."},"journalTitle":{"0":"Hepatology"},"pubYear":{"0":"2015"},"journalVolume":{"0":"61"},"issue":"","pageInfo":{"0":"361-374"}},{"hitCount":{"0":"3347"},"page":{"0":"1"},"pmid":{"0":"25480917"},"title":{"0":"Vangl2 cooperates with Rab11 and Myosin V to regulate apical constriction during vertebrate gastrulation."},"authors":{"0":"Ossipova O, Chuykin I, Chu CW, Sokol SY."},"journalTitle":{"0":"Development"},"pubYear":{"0":"2015"},"journalVolume":{"0":"142"},"issue":"","pageInfo":{"0":"99-107"}},{"hitCount":{"0":"3347"},"page":{"0":"1"},"pmid":{"0":"25831508"},"title":{"0":"TRAPPII regulates exocytic Golgi exit by mediating nucleotide exchange on the Ypt31 ortholog RabERAB11."},"authors":{"0":"Pinar M, Arst HN Jr, Pantazopoulou A, Tagua VG, de los R\u00edos V, Rodr\u00edguez-Salarichs J, D\u00edaz JF, Pe\u00f1alva MA."},"journalTitle":{"0":"Proc Natl Acad Sci U S A"},"pubYear":{"0":"2015"},"journalVolume":{"0":"112"},"issue":"","pageInfo":{"0":"4346-4351"}},{"hitCount":{"0":"3347"},"page":{"0":"1"},"pmid":{"0":"25305083"},"title":{"0":"Rab11 modulates \u03b1-synuclein-mediated defects in synaptic transmission and behaviour."},"authors":{"0":"Breda C, Nugent ML, Estranero JG, Kyriacou CP, Outeiro TF, Steinert JR, Giorgini F."},"journalTitle":{"0":"Hum Mol Genet"},"pubYear":{"0":"2015"},"journalVolume":{"0":"24"},"issue":"","pageInfo":{"0":"1077-1091"}},{"hitCount":{"0":"3347"},"page":{"0":"1"},"pmid":{"0":"25862161"},"title":{"0":"A novel role of Rab11 in trafficking GPI-anchored trans-sialidase to the plasma membrane of Trypanosoma cruzi."},"authors":{"0":"Niyogi S, Docampo R."},"journalTitle":{"0":"Small GTPases"},"pubYear":{"0":"2015"},"journalVolume":{"0":"6"},"issue":"","pageInfo":{"0":"8-10"}},{"hitCount":{"0":"3347"},"page":{"0":"1"},"pmid":{"0":"25673879"},"title":{"0":"The Arf and Rab11 effector FIP3 acts synergistically with ASAP1 to direct Rabin8 in ciliary receptor targeting."},"authors":{"0":"Wang J, Deretic D."},"journalTitle":{"0":"J Cell Sci"},"pubYear":{"0":"2015"},"journalVolume":{"0":"128"},"issue":"","pageInfo":{"0":"1375-1385"}},{"hitCount":{"0":"3347"},"page":{"0":"1"},"pmid":{"0":"25972173"},"title":{"0":"Synaptic Function of Rab11Fip5: Selective Requirement for Hippocampal Long-Term Depression."},"authors":{"0":"Bacaj T, Ahmad M, Jurado S, Malenka RC, S\u00fcdhof TC."},"journalTitle":{"0":"J Neurosci"},"pubYear":{"0":"2015"},"journalVolume":{"0":"35"},"issue":"","pageInfo":{"0":"7460-7474"}},{"hitCount":{"0":"3347"},"page":{"0":"1"},"pmid":{"0":"25632160"},"title":{"0":"Myosin Va mediates BDNF-induced postendocytic recycling of full-length TrkB and its translocation into dendritic spines."},"authors":{"0":"Sui WH, Huang SH, Wang J, Chen Q, Liu T, Chen ZY."},"journalTitle":{"0":"J Cell Sci"},"pubYear":{"0":"2015"},"journalVolume":{"0":"128"},"issue":"","pageInfo":{"0":"1108-1122"}},{"hitCount":{"0":"3347"},"page":{"0":"1"},"pmid":{"0":"25686250"},"title":{"0":"Early steps in primary cilium assembly require EHD1\/EHD3-dependent ciliary vesicle formation."},"authors":{"0":"Lu Q, Insinna C, Ott C, Stauffer J, Pintado PA, Rahajeng J, Baxa U, Walia V, Cuenca A, Hwang YS, Daar IO, Lopes S, Lippincott-Schwartz J, Jackson PK, Caplan S, Westlake CJ."},"journalTitle":{"0":"Nat Cell Biol"},"pubYear":{"0":"2015"},"journalVolume":{"0":"17"},"issue":"","pageInfo":{"0":"228-240"}},{"hitCount":{"0":"3347"},"page":{"0":"1"},"pmid":{"0":"25904794"},"title":{"0":"Protein Interacting with C-Kinase 1 Deficiency Impairs Glutathione Synthesis and Increases Oxidative Stress via Reduction of Surface Excitatory Amino Acid Carrier 1."},"authors":{"0":"Wang YN, Zhou L, Li YH, Wang Z, Li YC, Zhang YW, Wang Y, Liu G, Shen Y."},"journalTitle":{"0":"J Neurosci"},"pubYear":{"0":"2015"},"journalVolume":{"0":"35"},"issue":"","pageInfo":{"0":"6429-6443"}},{"hitCount":{"0":"3347"},"page":{"0":"1"},"pmid":{"0":"26041286"},"title":{"0":"Intracellular Transport of Vaccinia Virus in HeLa Cells Requires WASH-VPEF\/FAM21-Retromer Complexes and Recycling molecules Rab11 and Rab22."},"authors":{"0":"Hsiao JC, Chu LW, Lo YT, Lee SP, Chen TJ, Huang CY, Ping YH, Chang W."},"journalTitle":{"0":"J Virol"},"pubYear":{"0":"2015"},"journalVolume":"","issue":"","pageInfo":""},{"hitCount":{"0":"3347"},"page":{"0":"1"},"pmid":{"0":"25704914"},"title":{"0":"Mechanisms regulating cell membrane localization of the chemokine receptor CXCR4 in human hepatocarcinoma cells."},"authors":{"0":"Cepeda EB, Dediulia T, Fernando J, Bertran E, Egea G, Navarro E, Fabregat I."},"journalTitle":{"0":"Biochim Biophys Acta"},"pubYear":{"0":"2015"},"journalVolume":{"0":"1853"},"issue":"","pageInfo":{"0":"1205-1218"}},{"hitCount":{"0":"3347"},"page":{"0":"1"},"pmid":{"0":"26021297"},"title":{"0":"The small GTPase Rab29 is a common regulator of immune synapse assembly and ciliogenesis."},"authors":{"0":"Onnis A, Finetti F, Patrussi L, Gottardo M, Cassioli C, Span\u00f2 S, Baldari CT."},"journalTitle":{"0":"Cell Death Differ"},"pubYear":{"0":"2015"},"journalVolume":"","issue":"","pageInfo":""},{"hitCount":{"0":"3347"},"page":{"0":"1"},"pmid":{"0":"26004511"},"title":{"0":"Slitrk5 Mediates BDNF-Dependent TrkB Receptor Trafficking and Signaling."},"authors":{"0":"Song M, Giza J, Proenca CC, Jing D, Elliott M, Dincheva I, Shmelkov SV, Kim J, Schreiner R, Huang SH, Castr\u00e9n E, Prekeris R, Hempstead BL, Chao MV, Dictenberg JB, Rafii S, Chen ZY, Rodriguez-Boulan E, Lee FS."},"journalTitle":{"0":"Dev Cell"},"pubYear":{"0":"2015"},"journalVolume":{"0":"33"},"issue":"","pageInfo":{"0":"690-702"}},{"hitCount":{"0":"3347"},"page":{"0":"1"},"pmid":{"0":"25753037"},"title":{"0":"LTP-triggered cholesterol redistribution activates Cdc42 and drives AMPA receptor synaptic delivery."},"authors":{"0":"Brachet A, Norwood S, Brouwers JF, Palomer E, Helms JB, Dotti CG, Esteban JA."},"journalTitle":{"0":"J Cell Biol"},"pubYear":{"0":"2015"},"journalVolume":{"0":"208"},"issue":"","pageInfo":{"0":"791-806"}},{"hitCount":{"0":"3347"},"page":{"0":"1"},"pmid":{"0":"26110570"},"title":{"0":"Predominant Rab-GTPase amplicons contributing to oral squamous cell carcinoma progression to metastasis."},"authors":{"0":"da Silva SD, Marchi FA, Xu B, Bijian K, Alobaid F, Mlynarek A, Rogatto SR, Hier M, Kowalski LP, Alaoui-Jamali MA."},"journalTitle":{"0":"Oncotarget"},"pubYear":{"0":"2015"},"journalVolume":"","issue":"","pageInfo":""},{"hitCount":{"0":"3347"},"page":{"0":"1"},"pmid":{"0":"26067165"},"title":{"0":"Site-Specific Phosphorylation of VEGFR2 Is Mediated by Receptor Trafficking: Insights from a Computational Model."},"authors":{"0":"Clegg LW, Mac Gabhann F."},"journalTitle":{"0":"PLoS Comput Biol"},"pubYear":{"0":"2015"},"journalVolume":{"0":"11"},"issue":"","pageInfo":{"0":"e1004158"}},{"hitCount":{"0":"3347"},"page":{"0":"1"},"pmid":{"0":"25941134"},"title":{"0":"Mechanisms of apical-basal axis orientation and epithelial lumen positioning."},"authors":{"0":"Overeem AW, Bryant DM, van IJzendoorn SC."},"journalTitle":{"0":"Trends Cell Biol"},"pubYear":{"0":"2015"},"journalVolume":"","issue":"","pageInfo":""},{"hitCount":{"0":"3347"},"page":{"0":"1"},"pmid":{"0":"25394938"},"title":{"0":"The fungal pathogen Cryptococcus neoformans manipulates macrophage phagosome maturation."},"authors":{"0":"Smith LM, Dixon EF, May RC."},"journalTitle":{"0":"Cell Microbiol"},"pubYear":{"0":"2015"},"journalVolume":{"0":"17"},"issue":"","pageInfo":{"0":"702-713"}},{"hitCount":{"0":"3347"},"page":{"0":"1"},"pmid":{"0":"25800842"},"title":{"0":"3D time-lapse analysis of Rab11\/FIP5 complex: spatiotemporal dynamics during apical lumen formation."},"authors":{"0":"Mangan A, Prekeris R."},"journalTitle":{"0":"Methods Mol Biol"},"pubYear":{"0":"2015"},"journalVolume":{"0":"1298"},"issue":"","pageInfo":{"0":"181-186"}}]

尝试将标题强制转换为字符串值:

$title = (string) $xml->resultList->result[$i]->title;

尝试将标题强制转换为字符串值:

$title = (string) $xml->resultList->result[$i]->title;

这是因为标题实际上是一个
对象

"title":{
    "0":"Hypoxia upregulates Rab11-family interacting protein 4 through HIF-1\u03b1 to promote the metastasis of hepatocellular carcinoma."
}
如果无法更改输出,请尝试如下访问:

json_obj[0].title["0"];

但是如果标题来自您的php ready to use,那就更好了。在这种情况下,我看不出有任何理由返回数组。

这是因为标题实际上是一个
对象

"title":{
    "0":"Hypoxia upregulates Rab11-family interacting protein 4 through HIF-1\u03b1 to promote the metastasis of hepatocellular carcinoma."
}
如果无法更改输出,请尝试如下访问:

json_obj[0].title["0"];

但是如果标题来自您的php ready to use,那就更好了。在这种情况下,我看不出有任何理由返回数组。

问题在于PHP中的XML/JSON转换。我不确定您的XML结构,但您当前正在传递一个对象而不是一个值。基于以上@Stefan的答案,可能值得使用一个函数来简化流程:

// get the title, etc...    
$title = attr_val( $xml->resultList->result[$i]->title );

function attr_val ( $attr ) {
  return (string) ($attr ?: "");
}
您也可以在前端解决此问题:

var data=JSON.parse(responseText);
//迭代每个引用
对于(数据中的i){
//而且是属性
for(数据[i]中的属性){
//查找需要字符串的对象
if(数据[i][attr].hasOwnProperty(“0”)){
//替换为所需的字符串
数据[i][attr]=数据[i][attr][“0”];
}
}
}
//现在可以按预期访问所有属性
数据[0]。标题
//查看完整输出
document.querySelector('pre').innerHTML=JSON.stringify(数据,null,'\t')
var-responseText='[{“hitCount”:{“0”:“3347”},第页:{“0”:“1”},pmid:{“0”:“25745995”},标题:{“0”:“缺氧通过HIF-1\u03b1上调Rab11家族相互作用蛋白4以促进肝细胞癌的转移”。},作者:{“0”:“胡福,邓X,杨X,金H,顾D,吕X,王C,张Y,霍X,申Q,罗Q,赵F,葛T,赵F,朱W,舒H,姚M,范J,秦W.,,,《新闻报》:{“0”:“癌基因”},“出版年”:{“0”:“2015”},“新闻卷”;“发行”;“页面信息”;{“点击次数”;{“0”:“3347”;“页面”;{“0”:“1”;“pmid”;{“0”:“25835”;“标题”{Rab11对Rab8鸟嘌呤核苷酸交换因子Rabin8的动力学激活。},“作者:{“0”:“Feng S,Wu B,Per\u00e4nen J,Guo W.”,journalTitle:{“0”:“Methods Mol Biol”},“pubYear”:{“0”:“2015”},“journalVolume”:{“0”:“1298”},“发行号”:“pageInfo:{“0”:“99-106”},{“hitCountRab11和Rab14小GTP与其共享效应子Rab偶联蛋白(RCP)相互作用的结构-功能分析。“}”,作者:{“0”:“Lall P,Lindsay A,Hanscom S,Kecman T,Taglauer ES,McVeigh UM,Franklin E,McCaffrey MW,Khan AR.”,“journalTitle”:{“0”:“J Biol Chem”},pubYear:{“0”:“2015”,“journalVolume”:“问题”:”pageInfo:“},{”hitCount:“{”0:“3347”},第页:{”0:“1”},pmid:{”0:“25815277”},标题:{”0:“Rab11在疾病进展中”},作者:{”0:“Bhuin T,Roy JK.”,{”journalTitle:“{”0:“Int J Mol Cell Med”},pubYear:{”0:“2015”{”0:“journalVolume:{”0:“4”},第期:““pageInfo:{”0:“1-8”{”hitCount25800843“,标题:{“0”:“果蝇Rab11 GAP活性的体外和体内表征”,作者:{“0”:“Laflamme C,Emery G.”,“journalTitle”:{“0”:“Methods Mol Biol”},“pubYear”:{“0”:“2015”},“journalVolume”:{“0”:“1298”},第期:““pageInfo”:{“0”:“187 194”},{“hitCount”:“0”:“0”:“3347”},“Page0”:“pmid”{“24758”},{“0”标题:{“0”:“血管扩张剂刺激的磷蛋白通过调节转化生长因子β受体的Rab11依赖性质膜靶向来促进肝星状细胞的活化。”},“作者:{“0”:“Tu K,Li J,Verma VK,Liu C,Billadeau DD,Lamprecht G,Xiang,Guo L,Dhanasekaran R,Roberts LR,Shah VH,Kang N.”,《华尔街日报》“:{0:“国际肝病学”},出版年:{0:“2015”},期刊卷:{0:“61”},发行:“,“pageInfo:{0:“361-374”},{“hitCount:{”0:“3347”},第:{0:“1”},pmid:{0:“25480917”},标题:{“0:“Vangl2与Rab11和肌球蛋白V合作调节脊椎动物原肠胚形成过程中的顶端收缩。”},作者:“:“0”:”“Ossipova O,Chuykin I,Chu CW,Sokol SY.”,“journalTitle”:{“0”:“Development”},“pubYear”:{“0”:“2015”},“journalVolume”:{“0”:“142”},“issue”:“pageInfo”:{“0”:“99-107”},{“hitCount”:{“0”:“3347”},第页:{“0”:“1”},“pmid”:{“0”:“25831508”},“标题”:{“0”:”TRAPPII通过调节Ypt31直系基因Raberaba11上的核苷酸交换来调节外系高尔基体的退出。作者:{“0”:“Pinar M,Arst HN Jr,Pantazopoulou A,Tagua VG,de los R\u00edguez Salarichs V,Rodr\u00edguez Salarichs J,D\u00edaz JF,Pe\u00f1alva MA.”,“journalTitle”:“0”:“Proc Natl Acad Sci U S A”,pubYear:{“0”:“2015”:“journalVolume”:“0”:“0”:”“问题:”,“pageInfo:{0:“4346-4351”},{“hitCount:{”0:“3347”},第页:{0:“1”},pmid:{”0:“25305083”},标题:{”0:“Rab11调节突触传递和行为中的\u03b1突触核蛋白介导的缺陷。”},“作者:{”0:“Breda C,Nugent ML,Estranero JG,Kyriacou CP,Outeiro TF,Steinert JR,Giorgini F.”,Journal:{0:“,”Hum Mol Genet“}”,pubYear:{“0”:“2015”},journalVolume:{“0”:“24”},第期:“,”pageInfo:{“0”:“1077-1091”},{“hitCount:{“0”:“3347”},第页:{“0”:“1”},pmid:{“0”:“25862161”},标题:{“Rab11在将GPI锚定的反式唾液酸酶贩运到锥虫质膜中的新作用”,作者:“:”AmpniyogijournalTitle:{“0”:“Small GTPases”},pubYear:{“0”:“2015”},journalVolume:{“0”:“6”},问题:“,”pageInfo:{“0”:“8-10”},{“hitCount”:{“0”:“3347”},第页:{“0”:“1”},pmid:{“0”:“25673879”},标题:{“0”