Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON array convert to Javascript array

Hi. I got my output in JSON... Now I need to convert those data into javascript..

How to write the code in javascript? I have to display the images to the browser.. it is possible only by writing the code in javascript. Help me..

My JSON output is..

    [{"0":"101","member_id":"101","1":"3k.png","image_nm":"3k.png","2":"\/images\/phones\/","image_path":"\/images\/phones\/"},
     {"0":"102","member_id":"102","1":"mirchi.png","image_nm":"mirchi.png","2":"images\/phones\/","image_path":"images\/phones\/"},
     {"0":"103","member_id":"103","1":"masti.png","image_nm":"masti.png","2":"images\/phones\/","image_path":"images\/phones\/"}]
like image 991
krishna bhargavi Avatar asked Mar 06 '12 10:03

krishna bhargavi


People also ask

How do I convert a JSON object to an array?

Convert JSON to Array Using `json.The parse() function takes the argument of the JSON source and converts it to the JSON format, because most of the time when you fetch the data from the server the format of the response is the string. Make sure that it has a string value coming from a server or the local source.

Can a JSON object be an array?

JSON can be either an array or an object.

Can you JSON Stringify an array?

Stringify a JavaScript ArrayIt is also possible to stringify JavaScript arrays: Imagine we have this array in JavaScript: const arr = ["John", "Peter", "Sally", "Jane"]; Use the JavaScript function JSON.

What is JSON Stringify in JavaScript?

JSON.stringify() The JSON.stringify() method converts a JavaScript value to a JSON string, optionally replacing values if a replacer function is specified or optionally including only the specified properties if a replacer array is specified.


3 Answers

hai i got my output in JSON...now i need to convert those data into javascript..

Use JSON.parse() function in order to convert it to JS object.

var obj = JSON.parse(yourJsonString);

And now you can use for-in loop to iterate over each of its items:

for (var x in obj){
  if (obj.hasOwnProperty(x)){
    // your code
  }
}
like image 105
Sarfraz Avatar answered Oct 19 '22 05:10

Sarfraz


If you are using jQuery you can use

var object = $.parseJSON(jsonstring);

Or add this library https://raw.github.com/douglascrockford/JSON-js/master/json2.js and give

var object = JSON.parse(jsonstring);
like image 3
Diode Avatar answered Oct 19 '22 05:10

Diode


you should be able to use it as an object, which supports all of the key functions of an array

like image 2
Mild Fuzz Avatar answered Oct 19 '22 06:10

Mild Fuzz