26

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\/"}]

5 Answers 5

45

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
  }
}

7 Comments

where should i write this function?
in the place of yourJsonString i need to write the whole json array??
@krishnabhargavi: Yes you are write. If your json is stored in a variable, you can then write that there too.
@krishnabhargavi: Yes can write it where your other JS code is in the correct context of your json string.
@krishnabhargavi: For that problem you can ask another question (and accept one from here) because that is completely different problem.
|
4

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);

Comments

2

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

Comments

1

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);
}

4 Comments

Ohk..but i need the code to display the images to the browser.
@krishnabhargavi Where are the images stored?
@krishnabhargavi Please try to be more descriptive, where is this folder located? On your computer? On a server?
And where is that in relation to this file?
-1

Here are my two cents :

var my_json = [{created_at: "2017-03-14T01:00:32Z", entry_id: 33358, field1: "4", field2: "4", field3: "0"},{created_at: "2019-03-14T01:00:32Z", entry_id: 33359, field1: "4", field2: "4", field3: "0"}];

var data =[];
var dataSet=[];
	my_json.forEach((val,index)=>{
		if(my_json[index]!==null){
			for(var i in my_json[index]) {
					data.push(my_json[index][i]);
			}
			dataSet.push(data);
			data=[];
		}	
	})
console.log("...java Script Array... \n"+JSON.stringify(dataSet));

1 Comment

You are converting a JavaScript array of objects to a JavaScript array of arrays. This is not what Krishna asked for. Your variable my_json does not hold JSON data.

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.