0

This seems to be a very basic question, but it took my couple of hours as i am not much aware of javascript and jquery.

Well, i need to parse the Json string and get the value of particular key. Json looks like: {"currentCT":"Brijesh"}

I tried eval() and parse() but it seems i am missing something and i am not able to trace the issue.

I tried:

pre.innerHTML = eval('(' + res + ')');

and

pre.innerHTML = JSON.parse(res);

Both are not working for me.

Output Both methods returns [object Object]

Point out my mistake

EDIT

<script type="text/javascript">
    function findTeacher() {
        var url = "/TemplateTest/add/findclassteacher.action?";
        var standard = document.getElementById("classes_widget");
        var val_standard = "standard="
                + standard.options[standard.selectedIndex].value;
        url = url + val_standard + "&";
        var section = document.getElementById("sections_widget");
        var val_section = "section="
                + section.options[section.selectedIndex].value;
        url = url + val_section;
        var xmlhttp;
        if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {// code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                var res = xmlhttp.responseText;
                var pre = document.getElementById("holder");
                pre.innerHTML = JSON.parse(res);
                pre.innerHTML = eval('(' + res + ')');
            }
        }
        xmlhttp.open("GET", url, true);
        xmlhttp.send();

    }
</script>
Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
Gaurav Gupta
  • 4,586
  • 4
  • 39
  • 72
  • Are you getting any errors? How are you "getting" the JSON string? – Cerbrus Jun 03 '14 at 10:00
  • What is the exact error you can see in error console – Sandeeproop Jun 03 '14 at 10:01
  • I am getting json in response to ajax request. – Gaurav Gupta Jun 03 '14 at 10:01
  • Post your code! You have 1.8k rep, you should know by now that we need all relevant information to help you solve the problem. We need your code that requests the JSON, and the __exact__ contents of the JSON. – Cerbrus Jun 03 '14 at 10:03
  • @Cerbrus I posted the relevant code only, the json i mentioned is the one i received as response and only part of code that is included in parsing json is `eval` and `parse`. So i posted this much info only. If request is also relevant for you i will post that too. – Gaurav Gupta Jun 03 '14 at 10:07
  • @Teemu I tried your answer and it is working fine for me. :) – Gaurav Gupta Jun 03 '14 at 10:12

2 Answers2

1

I'd forget the eval(). You have also to read the value, not just an object:

pre.innerHTML = JSON.parse(res).currentCT;

JSON.parse(res) returns an object, innerHTML expects a string as a value. If the assigned value is an object instead, its toString() method is used, which returns [object Object] in your case.

Teemu
  • 22,918
  • 7
  • 53
  • 106
-1

try this with your code

$.getJSON( "ajax/test.json", function( data ) {
    var _data = JSON.parse(data);
    console.log(_data.currentCT) // Brijesh
});
Jeetendra Chauhan
  • 1,977
  • 2
  • 15
  • 21