最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to pass a set of json data in javascript function? - Stack Overflow

programmeradmin2浏览0评论

I have a data set as below:

data = '{"a":1,"b":2,"c":3},{"a":4,"b":5,"c":6},{"a":7,"b":8,"c":9}';

I am trying to make a function with the data set as its parameter, but the parameter wouldn't be read. Here is what I did:

function add(data) { alert(data); } add(data);

I only get [object Object],[object Object] ... What's the problem here? Thanks.

I have a data set as below:

data = '{"a":1,"b":2,"c":3},{"a":4,"b":5,"c":6},{"a":7,"b":8,"c":9}';

I am trying to make a function with the data set as its parameter, but the parameter wouldn't be read. Here is what I did:

function add(data) { alert(data); } add(data);

I only get [object Object],[object Object] ... What's the problem here? Thanks.

Share Improve this question asked Jun 2, 2016 at 9:33 D KimD Kim 1071 gold badge1 silver badge11 bronze badges 3
  • 1 try to console instead of alert, you will get your expected result – Mahedi Sabuj Commented Jun 2, 2016 at 9:34
  • There's no problem. You are passing a list of objects and you're receiving it. – emerson.marini Commented Jun 2, 2016 at 9:34
  • alert tries to convert data into string, and object.toString() is "[object Object]", so try extracting the value from data and then do alert. – Rahul R. Commented Jun 2, 2016 at 9:40
Add a comment  | 

5 Answers 5

Reset to default 6

The JSON string is wrong. It should be actually:

var data = '[{"a":1,"b":2,"c":3},{"a":4,"b":5,"c":6},{"a":7,"b":8,"c":9}]';

After that, you need to convert the JSON String into JSON object using the code below:

JSON.parse(d) /* d is the parameter of the method 'add()'  */

The alert will give you [object Object] output, as the variable data is itself the object. So if you want to see the whole json data, you need to console.log as:

console.log(JSON.parse(d));

Watch the demo.

First of all, your data value is incorrect. Since it has 3 objects it has to be in an array. So, your data should be

data = '[{"a":1,"b":2,"c":3},{"a":4,"b":5,"c":6},{"a":7,"b":8,"c":9}]';

Then you need to use JSON.parse function to parse the string data into javascript object and then pass the object.

function add(data)
{ 
   alert(data);
   alert(data[0].a); //access 1ts objects a value
} 
var data = JSON.parse(data);
add(data);

First you should pass the function an object parameter and you can store the object in an array:

var data = []; //array
function add(d) {
 data.push(d); //add passed item to array
 console.dir(data); //this will print the whole object
}

add({"a":1,"b":2,"c":3});

console.dir() will make you print the properties within the object.

You can use JSON.stringify().

The data needs to have a JSON structure however.

So in your case that would be:

var data = [{"a":1,"b":2,"c":3},{"a":4,"b":5,"c":6},{"a":7,"b":8,"c":9}]
add(JSON.stringify(data))

[{"a":1,"b":2,"c":3},{"a":4,"b":5,"c":6},{"a":7,"b":8,"c":9}] //output of JSON.Stringify()

am also getting same issue few hours later and I fixed it using JSON.parse(data)..Like bellow coding

var data = '[{"a":1,"b":2,"c":3},{"a":4,"b":5,"c":6},{"a":7,"b":8,"c":9}]';
var obj = JSON.parse(data);
obj.forEach(myFunction);
function myFunction(item, index) {
     var eachid = item.id;
     console.log("Item-id", eachid);//you will get id 
     add(eachid);
} 

you are directly pass the data .so only you get the [objcet Object]...if you using JSON.parse(data) ,You will get the id of each item

发布评论

评论列表(0)

  1. 暂无评论