-2

I am returning array as

$array = array {
         'id' => 1,
         'name'=>krishna,
}
echo json_encode($array);
exit;

from an ajax call

How can I convert this json value to java script array?

This is my actual data

var data = [{
   "candidate_notes_id":"1",
    "candidate_id":"38",
    "subject":"test",
    "description":"t‌estestsete\netestes\n\n\nsteetet",
    "private":"0",
    "created_date":"2012-09-14 11:55:13",
    "updated_date":"2012-09-14 11:55:13",
    "updated_by":"admin"
  }] 

 var newArray = jQuery.parseJSON(data); 
 alert(newArray);
 return false; 

result :

                      var newArray = JSON.stringify(data);
          var date_split = newArray.substr(1,newArray.length-2);
          var newData = date_split.replace('\n','<br>');
          var newArray = $.parseJSON(newData); 
          alert(newArray.candidate_notes_id);
          alert(newArray.candidate_id);
          alert(newArray.subject);
          alert(newArray.description);
4

3 Answers 3

0

If you are using jQuery then you can use jQuery.parseJSON(YOUR_AJAX_RESPONSE_DATA); which will convert json to JS object

Link: http://api.jquery.com/jQuery.parseJSON/

5
  • This is my actual data var data = [{"candidate_notes_id":"1","candidate_id":"38","subject":"test","description":"testestsete\netestes\n\n\nsteetet","private":"0","created_date":"2012-09-14 11:55:13","updated_date":"2012-09-14 11:55:13","updated_by":"admin"}] var newArray = jQuery.parseJSON(data); alert(newArray);return false; The Return value is null
    – krishnaraj
    Commented Sep 14, 2012 at 8:07
  • Control characters like \n need to be escaped. Look at your error console. "Bad control character in string literal". The \n have to be \\n
    – devnull69
    Commented Sep 14, 2012 at 8:18
  • This is the ajax response? {"candidate_notes_id":"1","candidate_id":"38","subject":"test","description":"t‌​estestsete\netestes\n\n\nsteetet","private":"0","created_date":"2012-09-14 11:55:13","updated_date":"2012-09-14 11:55:13","updated_by":"admin"} Commented Sep 14, 2012 at 8:19
  • I think you trying to get as array but they are in object. I modified your code and here it is, jsfiddle.net/25sjh Also you need to escape \n as \\n. I think you can do this in PHP side. Commented Sep 14, 2012 at 8:23
  • Ok. If my answer help you can set it as correct answer and give rep points. Commented Sep 14, 2012 at 10:39
0

Please look at an answered question ...

You will find how to convert a json to an array.

JSON to javaScript array

var array = [];
$.each(JSONObject, function(i, obj) {
    array.push([obj.id.value, obj.name.value]);
});
0

You can parse it using

obj = JSON.parse(responseData); // replace `responseData` with your XHR response variable name

in your success callback function. Then convert it an array as follows

var myArray=[];
myArray[0]=obj.id;
myArray[1]=obj.name;

but first of all your

$array = array {
    'id' => 1,
    'name'=>krishna,
};

should be

$array = array (
    'id' => 1,
    'name'=>'krishna'
);

DEMO.

2
  • When i am using JSON.parse() its through the error like SyntaxError: JSON.parse: unexpected character
    – krishnaraj
    Commented Sep 14, 2012 at 8:17
  • 1
    And 'krishna' should be quotes, assuming it's not a constant.
    – Sherlock
    Commented Sep 14, 2012 at 8:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.