This is my JSON string generated by C# JSON parser:
{
"NewDataSet": {
"Table": [
{
"ResultId": "1",
"AttachmentId": "1",
"AttachmentName": "Report1",
"RowsCount": "34",
"NotifyUserName": "william",
"InsBy": "developer",
"InsAt": "2012-12-07T17:28:01.46+08:00",
"IsNotify": "false"
},
{
"ResultId": "2",
"AttachmentId": "2",
"AttachmentName": "Report2",
"RowsCount": "37",
"NotifyUserName": "william",
"InsBy": "developer",
"InsAt": "2012-12-07T17:28:15.57+08:00",
"IsNotify": "false"
},
{
"ResultId": "3",
"AttachmentId": "3",
"AttachmentName": "Report3",
"RowsCount": "69",
"NotifyUserName": "william",
"InsBy": "developer",
"InsAt": "2012-12-07T17:28:25.58+08:00",
"IsNotify": "false"
}
]
}
}
Then I would like to parse the string to front end JavaScript to iterate the value.
I did this way.
var jsonText;
$.ajax({
type: "POST",
url: "Default.aspx/MethodWithNoParameterJSON",
data: {},
contentType: "application/json;charset=utf-8",
dataType: "json",
async: true,
cache: false,
success: function (msg) {
//rulesName = dbtitle+msg.d;
//rulesCount = +msg.d;
jsonText = msg.d;
alert(jsonText.NewDataSet.Table[0].ResultId),
},
error: function (x, e) {
alert("The call to the server side failed. " + x.responseText);
}
});
How can I get the child element data like jsonText.NewDataSet.Table[0].ResultId? Whenever I call alert(jsonText.NewDataSet.Table[0].ResultId), it will always prompt null or undefined object.
Why are you using
msg.dto set yourjsonTextvariable? Where does the.dproperty come from? Themsgparameter should already be the object created from your JSON response. Try this instead:(And note that your
jsonTextvariable is badly named: what you have at that point is not JSON or “text”, it is an object – or, in your case,undefinedbecausemsg.dis undefined. But you are trying to use it as an object, not as JSON.)