0

I have Converted a C# class in Json Object after it I stringify that json Object then it converts with backslash(escaping character) and when i used following code to read the json string it gives me undefined message in alert .

<script>



    var jsString = { "JsonString": ["{\"reachoutid\":1,\"user_ID\":1,\"question\":\"Best CMS in Microsoft  .Net\",\"anonymous\":false,\"shared\":false,\"create_date\":\"\\/Date(-62135596800000)\\/\",\"update_date\":\"\\/Date(1327300086183)\\/\",\"status\":0,\"start_date\":\"\\/Date(-62135596800000)\\/\",\"end_Date\":\"\\/Date(-62135596800000)\\/\",\"reachoutfactorsList\":null,\"reachoutchoicesList\":[{\"reachoutid\":0,\"choice_ID\":4,\"orders\":0,\"description\":\"Sitecore\",\"preachOutID\":0,\"pchoice_ID\":4,\"porders\":0,\"pdescription\":\"Sitecore\"},{\"reachoutid\":0,\"choice_ID\":5,\"orders\":0,\"description\":\".Net Nuke \",\"preachOutID\":0,\"pchoice_ID\":5,\"porders\":0,\"pdescription\":\".Net Nuke \"},{\"reachoutid\":0,\"choice_ID\":6,\"orders\":0,\"description\":\"Drupal\",\"preachOutID\":0,\"pchoice_ID\":6,\"porders\":0,\"pdescription\":\"Drupal\"}],\"detail\":\"Write more text to check progress bar work properly.\",\"set_Listof_Tags\":null,\"tag\":null,\"get_Listof_Tags\":null,\"userstatus\":null,\"actType\":\"RO\"}"], "_getList_MyActivities": null, "_getList_MyPeersActivities": null, "userID": 1 }

    for (i = 0; jsString.JsonString.length > i; i++) 
    {

        alert(jsString.JsonString[i].reachoutid);

        //"Giving Undefined Message "

    }

</script>

2 Answers 2

2

Your JSON is stored as a string, not as a native object. To convert it back, change your alert( ... ) line to use JSON.parse( ... ) like this:

    alert( JSON.parse(jsString.JsonString[i]).reachoutid )
Sign up to request clarification or add additional context in comments.

Comments

0

Your JSON shouldn't be quoted. When it is, JS interprets is as a string instead of an object

var jsObject = { "JsonString": [{"reachoutid":1,"user_ID":1,"question":"Best CMS in Microsoft  .Net","anonymous":false,"shared":false}]} // etc.

4 Comments

But when i converted my C# class object into Json like this it convert it with Backslash , JavaScriptSerializer objSerializer = new JavaScriptSerializer(); JSONString = objSerializer.Serialize(_ReachOut);
@Shailesh what type of object is _ReachOut?
Thanks for ur help ...got solution alert( JSON.parse(jsString.JsonString[i]).reachoutid )
JavaScript Object != JSON. JSON is a string. This answer fundamentally doesn't understand the differences between the two. Billy's answer is far more correct.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.