0

Can not parse json using this

var res = eval('(' + response + ')');

The response contain [{"total_sections":"3"}] this value. But i couldn't take the value total_sections.

function changeSection() { 
   id = document.getElementById('layoutDropdown').value; 
   $.ajax({ type: "POST",
            url:'/manage',
            data: {id : id },
            success: function(response){ 
                  alert(response); 
                  var res = eval(response);
                  alert(res[0].total_sections);                     
            } 
    }); 
} 

manage Layouts:

public function manageLayouts() { 
      $id = $_POST['id']; 
      $data = $this->managelayoutmodel->getSections($id);
      echo json_encode($data);
}
Christoph
  • 50,121
  • 21
  • 99
  • 128
keerthi
  • 769
  • 2
  • 9
  • 24
  • you should refrain from using the function eval(), read here why: http://stackoverflow.com/questions/86513/why-is-using-javascript-eval-function-a-bad-idea and here http://javascript.crockford.com/code.html – Henrik Andersson Apr 03 '12 at 05:56
  • What is the result of `alert(typeof response);` before calling the eval? – scessor Apr 03 '12 at 06:05
  • guess, you just missed the `[0]` in your call? It should be accessable by `res[0].total_sections` – Christoph Apr 03 '12 at 06:09

3 Answers3

1

You should always try to avoid using eval, it's evil!

You have 2 options:

If response is a string:

var res = JSON.parse(response);

or if it is already a JSON-Object (e.g. by setting the return type of you ajax call to json)

var res = response;

then you can access total sections by res[0].total_sections.

edit: your code has several problems:

add var before id = document.getElementById('layoutDropdown').value;. Sidenote: You are using jQuery, so this would be easier to access like this: $("#layoutDropdown").val();

do you run into the alert(response)? If not, your ajax call was not successfull. You should add an error Block to catch errors: http://api.jquery.com/jQuery.ajax/

Do a console.log(response) and examine the result. Does it have the proper values you want it to have? (You also can check body of the ajax-call for that).

Most likely, neither the eval(response) nor the accessing res[0].total_sections are the parts where your trouble comes from.

Christoph
  • 50,121
  • 21
  • 99
  • 128
  • I just tried using var res = JSON.parse(response); this.. But didnt get – keerthi Apr 03 '12 at 06:11
  • post your whole code, so we can see, what you are trying to do. – Christoph Apr 03 '12 at 06:16
  • – keerthi Apr 03 '12 at 06:18
  • public function manageLayouts() { $id = $_POST['id']; $data = $this->managelayoutmodel->getSections($id); echo json_encode($data); } – keerthi Apr 03 '12 at 06:18
  • @Anupama I edited my answer. Follow the steps and try if you can solve the problem. – Christoph Apr 03 '12 at 06:34
0

Try using JSON.parse(response) in newer browsers. For older browsers use json2.js and JSON.parse(response)

danwellman
  • 9,068
  • 8
  • 60
  • 88
0

If response is a string you can parse the json with var res = eval(response); (but it isn't nice), var res = JSON.parse(response); or if you use jQuery var res = $.parseJSON(response). Otherwise if it is already an object you don't need to parse it, only set var res = response;.
Now you can get the total sections by calling res[0].total_sections, e.g. you can test it with alert(res[0].total_sections);.

scessor
  • 15,995
  • 4
  • 43
  • 54
  • @Anupama: You're welcome. It would be nice if you could [accept the answer](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) of all your questions. – scessor Apr 03 '12 at 06:16
  • @scessor stop pushing users into accepting answers when the problem isn't even solved... – Christoph Apr 03 '12 at 06:25
  • 1
    @Christoph: 1.) I said "all of his questions" 2.) his comment pointed out that the problem was solved. – scessor Apr 03 '12 at 06:29