-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);
krishnaraj
  • 25
  • 3
  • Why don't you mind to ask Google exactly the same question? You don't know what Google is? [how can i convert this json value to java script array](http://www.google.com/webhp?hl=ru&tab=ww&authuser=0#hl=ru&safe=off&authuser=0&output=search&sclient=psy-ab&q=how+can+i+convert+this+json+value+to+java+script+array&oq=how+can+i+convert+this+json+value+to+java+script+array&gs_l=hp.3...1099.1099.0.1934.1.1.0.0.0.0.214.214.2-1.1.0...0.0...1c.1j2.48sKiECMHuA&pbx=1&bav=on.2,or.r_gc.r_pw.r_cp.r_qf.&fp=b9d969de151e492a&biw=1273&bih=787) – Alexander Larikov Sep 14 '12 at 08:05
  • Please be more specific. Do you want to have a Javascript array [1, "krishna"] or a Javascript object {id:1, name: "krishna"} ? – devnull69 Sep 14 '12 at 08:06
  • possible duplicate of [How to parse JSON in JavaScript](http://stackoverflow.com/questions/4935632/how-to-parse-json-in-javascript) – Quentin Sep 14 '12 at 08:12
  • Can you show the function from which you return false? – mplungjan Sep 14 '12 at 08:35

3 Answers3

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/

Muthu Kumaran
  • 17,682
  • 5
  • 47
  • 70
  • 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 Sep 14 '12 at 08: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 Sep 14 '12 at 08: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"}` – Muthu Kumaran Sep 14 '12 at 08:19
  • I think you trying to get as array but they are in object. I modified your code and here it is, http://jsfiddle.net/25sjh/ Also you need to escape `\n` as `\\n`. I think you can do this in PHP side. – Muthu Kumaran Sep 14 '12 at 08:23
  • Ok. If my answer help you can set it as correct answer and give rep points. – Muthu Kumaran Sep 14 '12 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]);
});
Community
  • 1
  • 1
Aelios
  • 11,849
  • 2
  • 36
  • 54
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.

The Alpha
  • 143,660
  • 29
  • 287
  • 307