Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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\/"}]
share|improve this question

5 Answers

up vote 2 down vote accepted

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
  }
}
share|improve this answer
where should i write this function? – krishna bhargavi Mar 6 '12 at 10:19
@krishnabhargavi: see the example code i have provided. – Sarfraz Mar 6 '12 at 10:20
in the place of yourJsonString i need to write the whole json array?? – krishna bhargavi Mar 6 '12 at 10:21
1  
@krishnabhargavi: Yes can write it where your other JS code is in the correct context of your json string. – Sarfraz Mar 6 '12 at 10:34
1  
@krishnabhargavi: For that problem you can ask another question (and accept one from here) because that is completely different problem. – Sarfraz Mar 6 '12 at 10:42
show 4 more comments

As Sarfraz is saying,

var jsonString = '[{"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\/"}]';
var obj = JSON.parse(jsonString);
// obj now contains the array!

EDIT: For it to display the images:

for (var i = 0, len = obj.length; i < len; i++){
    var img = new Image();
    img.setAttribute("src",obj[i][2] + obj[i][1]);
    document.body.appendChild(img);
}
share|improve this answer
Ohk..but i need the code to display the images to the browser. – krishna bhargavi Mar 6 '12 at 10:26
i used the code but images are not displayed. – krishna bhargavi Mar 6 '12 at 10:38
@krishnabhargavi Where are the images stored? – TheJohlin Mar 6 '12 at 10:40
in one of the folder... – krishna bhargavi Mar 6 '12 at 10:43
@krishnabhargavi Please try to be more descriptive, where is this folder located? On your computer? On a server? – TheJohlin Mar 6 '12 at 10:44
show 5 more comments

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

share|improve this answer

Please have a look here it might help you

http://stackoverflow.com/a/5618584/1149024
share|improve this answer

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);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.