result= {
"_id": "5319b5e10748a6078fe4f360",
"acces": "172.1.6.2.18",
"results": [{
"\"test\":\"Connect_Disconnect\",\"os\":\"Windows NT\",\"report\":\"Verify Wireless Interface present and state is Disconnected:OK<br>Verify Profile not present on the Client:OK<br>Verify the State is Disconnected:OK<br>Delete Profile to the Client:OK<br>Verify Profile Not Present on the Client:OK<br>\"": ""
}],
"tests": "Test01"
}
в то время как я пытаюсь предупредить следующее
alert(JSON.stringify(result.results[0]));
я получаю следующие данные
{
"\"test\":\"Connect_Disconnect\",\"os\":\"Windows NT\",\"report\":\"Verify Wireless Interface present and state is Disconnected:OK<br>Verify Profile not present on the Client:OK<br>Add Profile to the Client:OK<br>Verify Profile Added Present on the Client:OK<br>Connecting to Access Point:OK<br>Verify the State is Connected:OK<br>Disconnecting from Access Point:OK<br>Verify the State is Disconnected:OK<br>Delete Profile to the Client:OK<br>Verify Profile Not Present on the Client:OK<br>\"": ""
}
от этого как я могу получить значения test,os and report
Я пытался
result.results[0].test
но получение значения как undefined.
Вот обновленная часть
$.get('/getStatus', getdata, function (data) {
data.forEach(function (testreport) {
var report = JSON.stringify(testreport);
alert(report);
});
});
here alert prints
{
"_id": "5319b5e10748a6078fe4f360",
"acces": "172.1.6.2.18",
"adapter": "Win 10",
"flavour": "VM-IE8-001-preq1",
"id": "67",
"os": "VM-WIN7-64",
"results": [{
"\"test\":\"Connect_Disconnect\",\"os\":\"Windows NT\",\"report\":\"Verify Wireless Interface present and state is Disconnected:OK<br>Verify Profile not present on the Client:OK<br>Add Profile to the Client:OK<br>Verify Profile Added Present on the Client:OK<br>Connecting to Access Point:OK<br>Verify the State is Connected:OK<br>Disconnecting from Access Point:OK<br>Verify the State is Disconnected:OK<br>Delete Profile to the Client:OK<br>Verify Profile Not Present on the Client:OK<br>\"": ""
}],
"tests": "Test01"
}
Ваш результат. Получает строку, а не json.
проверить DEMO
var results = {
"\"test\":\"Connect_Disconnect\",\"os\":\"Windows NT\",\"report\":\"Verify Wireless Interface present and state is Disconnected:OK<br>Verify Profile not present on the Client:OK<br>Add Profile to the Client:OK<br>Verify Profile Added Present on the Client:OK<br>Connecting to Access Point:OK<br>Verify the State is Connected:OK<br>Disconnecting from Access Point:OK<br>Verify the State is Disconnected:OK<br>Delete Profile to the Client:OK<br>Verify Profile Not Present on the Client:OK<br>\"": ""
};
var resultsValid = {
"test": "\"Connect_Disconnect\"",
"os": "\"Windows NT\""
};
alert(results.test)
alert(resultsValid.test)
alert(resultsValid.os)
Проверьте, как вы генерируете этот ответ
Кроме того, вы можете проверить данные json на http://jsonlint.com/
Можете ли вы просто попробовать использовать метод jQuery getJSON()
и реализовать его следующим образом:
$.getJSON( "enterYourUrlHere", function( result ) {
alert(result.results[0].test)
});
И, конечно, @Mate верен.
это объект, а не массив. нет индекса 0, просто хеш или свойства. использовать это:
result.test
result.results
содержит 1 элемент, объект с 1 свойством и длинной строкой в качестве имени свойства:"\"test\":\"Connect_Disconnect\",\"os\":\"Windows NT\",\"report\":\"Verify Wireless Interface present and state is Disconnected:OK<br>Verify Profile not present on the Client:OK<br>Add Profile to the Client:OK<br>Verify Profile Added Present on the Client:OK<br>Connecting to Access Point:OK<br>Verify the State is Connected:OK<br>Disconnecting from Access Point:OK<br>Verify the State is Disconnected:OK<br>Delete Profile to the Client:OK<br>Verify Profile Not Present on the Client:OK<br>\""
.""
. Вы можете увидеть это здесь: jsfiddle.net/4KUVq/1 . Другими словами, ваша структура технически правильна, но не то, что вам нужно.