我有以下json文件,正在阅读下面的代码。
{
"17.216.30.177": {
"agent": {
"agent_ip": "17.216.30.177",
"agent_cpu_type": "Intel Core i5",
"agent_serial_num": "C02KQ00CFH46",
"agent_hostname": "Beautiful-iMac.local",
"agent_ram": "16 GB",
"agent_processors": "4",
"agent_system_type": "iMac14_2"
},
"devices": {
"fedaa89792fdf9bcfe819cc9981bcda918dc1fa6": {
"SerialNumber": "",
"HardwareModel": "abc",
"WiFiAddress": "abc",
"BuildVersion": "abc",
"kanzi": "315D0F",
"current_state": 1,
"UniqueChipID": "612806844428"
},
"47d46975e929d679f1c0713f7b060bf80390aeb9": {
"SerialNumber": "",
"HardwareModel": "lmn",
"WiFiAddress": "lmn",
"BuildVersion": "lmn",
"kanzi": "315DF3",
"current_state": 1,
"UniqueChipID": "2572890213651"
}
},
"last_alive_time": "2016-04-27 10:24:14"
},
"17.153.73.241": {
"agent": {
"agent_hostname": "aj-air.local",
"agent_cpu_type": "Intel Core i5",
"agent_processors": "2",
"agent_ip": "17.244.49.99",
"agent_serial_num": "C02M300KFY3V",
"agent_system_type": "MacBookAir6_2",
"agent_ram": "8 GB"
},
"devices": {
"ccd457202545bef923e2d784f0aa30e12c4dfd43": {
"HardwareModel": "pqr",
"kanzi": "pqr",
"SerialNumber": "",
"current_state": 1,
"UniqueChipID": "pqr",
"WiFiAddress": "pqr",
"BuildVersion": "pqr"
}
},
"last_alive_time": "2016-04-27 10:30:08"
}
}
function readTextFile(file, callback) {
var rawFile = new XMLHttpRequest();
rawFile.overrideMimeType("application/json");
rawFile.open("GET", file, true);
rawFile.onreadystatechange = function() {
if (rawFile.readyState === 4 && rawFile.status == "200") {
callback(rawFile.responseText);
}
}
rawFile.send(null);
}
readTextFile("/resources/test.json", function(text){
var data = JSON.parse(text);
document.getElementById("demo").innerHTML = data[0]["agent"].agent_ip;
});
data[0].17.216.30.177["agent"].agent_ip
;
我该怎么做才能做到这一点?
如果它是data[0].xyz["agent"].agent_ip;
,它运作良好,但因为ip期间,它不会
答案 0 :(得分:2)
我能够将json文件读入对象但无法获取特定的键值。我从服务器那里得到这个文件因此不能有[]的数组,如果那个&#39 ;; s引起了问题。
这个问题是你像数组一样访问它,但你没有一个对象数组。因此,您可以将收到的文件转换为数组,也可以按原样获取对象并更改访问它们的方式。
您的主要问题是每个人都有一个唯一的密钥,您需要引用该密钥才能访问数据。如果您提前知道它们的键,则将它们用作关联数组。您可以通过'来访问每个酒店。'运营商。
data[key].agent.agent_ip
否则,您需要找到每个密钥并使用它来访问该对象。
using Microsoft.Exchange.WebServices.Data;
protected void EmailEws()
{
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
service.Credentials = new WebCredentials("user", "password", "domain(local)");
service.TraceEnabled = true;
service.TraceFlags = TraceFlags.All;
// EWS service url.
service.Url = new Uri("https://example.com/EWS/Exchange.asmx");
EmailMessage email = new EmailMessage(service);
email.ToRecipients.Add("a@b.com");
email.Subject = "HelloWorld";
email.Body = new MessageBody("<p>This is the first email I've sent by using the EWS Managed API!</p>");
// set Body before BodyType or an error is raised!
email.Body.BodyType = BodyType.HTML;
email.SendAndSaveCopy();
}
我已经把它放在jsfiddle中了解如何用这种情况下的密钥访问对象。
参考文献:
答案 1 :(得分:0)
readTextFile("/resources/test.json", function(text){
var data = JSON.parse(text);
for (var key in data) {
console.log("Agent IP-->", data[key].agent.agent_ip);
console.log("Number of devices -->", Object.keys(data[key].devices).length);
console.log("Total Number of devices -->", Object.keys(data[key].devices).length);
}
这对我有用。