JSON字符串数组始终为null

时间:2016-02-11 20:49:17

标签: arrays json

我在服务器端呈现了一些JSON数据,可以在客户端页面中使用。在HTTP响应中,它看起来是正确的:

script_name

但是当我通过Dev Tools Elements或Console检查DOM时,上面JSON中的searchFilters字符串数组始终为null。我根本没有引用该对象的客户端代码。如何/为什么它从HTTP响应中的正确变为DOM中的null?我已经在JSONLint中验证了JSON,它是有效的。

1 个答案:

答案 0 :(得分:0)

JSON有效。添加方括号,然后重试。

var objArray= [{
	"page": [{
		"pageInfo": [{
			"geoRegion": "US",
			"onsiteSearchResults": 38,
			"onsiteSearchTerm": "leather",
			"sysEnv": "HAMP"
		}],
		"category": [{
			"dirID": 2,
			"pageTemplate": "/store/search.aspx",
			"pageType": "Search"
		}],
		"attributes": [{
			"searchType": "standard",
			"refinementCategory": "10001",
			"searchFilters": ["50859"]
		}]
	}],
	"user": [{
		"profile": {
			"profileInfo": {
				"profileID": "{C0A253A9-AD6E-4B6B-A313-3D215704D0FB}",
				"returningStatus": "new"
			}
		},
		"segment": {
			"persona": ["EXPERT"]
		}
	}],
	"cart": [{
		"cartID": "2508d65f-6495-4256-8125-75767b847e45",
		"numItems": 0
	}],
	"version": "1.0"
}];


function getDetails(){
	
	document.getElementById("demo").innerHTML = "Length of Array = " + objArray.length;
	document.getElementById("element").innerHTML = "0th Element of Array = " + objArray[0];
	document.getElementById("elementInfo").innerHTML = "Element at 0th position Array = " + objArray[0].page[0].pageInfo;	
	document.getElementById("elementInfo2").innerHTML = "Element at 00th position Array = " + objArray[0].page[0].pageInfo[0].geoRegion;

}
<button onclick=getDetails()> Check </button>

<p id="demo"></p>
<p id="element"></p>
<p id="elementInfo"></p>
<p id="elementInfo2"></p>