-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
  • 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 Commented Sep 14, 2012 at 8:05
  • Please be more specific. Do you want to have a Javascript array [1, "krishna"] or a Javascript object {id:1, name: "krishna"} ? Commented Sep 14, 2012 at 8:06
  • possible duplicate of How to parse JSON in JavaScript Commented Sep 14, 2012 at 8:12
  • Can you show the function from which you return false? Commented Sep 14, 2012 at 8:35

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/

Sign up to request clarification or add additional context in comments.

5 Comments

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
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
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"}
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.
Ok. If my answer help you can set it as correct answer and give rep points.
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]);
});

Comments

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 Comments

When i am using JSON.parse() its through the error like SyntaxError: JSON.parse: unexpected character
And 'krishna' should be quotes, assuming it's not a constant.

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.