Having trouble returning a string from my server that needs to be parsed into a JavaScript object. However, encountering errors during the parsing process and unable to figure out why. Perhaps there is something I'm missing.
The format of my string is as follows:
{{"fname":"bob","lname":"jones"},{...}}
My intention was to parse it like this:
var item = JSON.parse(myString);
This should create an array of names in 'item', allowing me to do something like:
for(var i = 0; i < item.length; i++){
alert(item[i].fname + " " + item[i].lname);
}
Could you point out if there's any mistake in the above approach? The following snippet shows actual code being used:
while (reader.Read())
{
if (reader["rt_id"] != DBNull.Value && reader["rt_name"] != DBNull.Value)
{
t = @"{""pValue"":""{ReportType},"+reader["rt_id"]+@""",""pText"":"""+reader["rt_name"]+@"""}";
returnContentsArray.Add(t);
}
}
returnContents = "{" + String.Join(",",returnContentsArray.ToArray()) + "}";
return returnContents;
On Client-side:
var item = JSON.parse(result);