Javascript 如何在javascript函数中传递一组json数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37587943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass a set of json data in javascript function?
提问by D Kim
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}';
数据 = '{"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.
我只得到 [object Object],[object Object] ... 这里有什么问题?谢谢。
回答by Shashank
The JSON string is wrong. It should be actually:
JSON 字符串错误。其实应该是:
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 JSONString into JSONobject using the code below:
之后,您需要使用以下代码将JSON字符串转换为JSON对象:
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:
该alert
给你[object Object]
输出,作为变量data
本身的对象。因此,如果您想查看整个json
数据,则需要console.log
:
console.log(JSON.parse(d));
Watch the demo.
观看演示。
回答by Karthik M R
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
首先,您的数据值不正确。因为它有 3 个对象,所以它必须在一个数组中。所以,你的数据应该是
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.
然后你需要使用 JSON.parse 函数将字符串数据解析为 javascript 对象,然后传递该对象。
function add(data)
{
alert(data);
alert(data[0].a); //access 1ts objects a value
}
var data = JSON.parse(data);
add(data);
回答by Pakkiya Nathan
am also getting same issue few hours later and I fixed it using JSON.parse(data)..Like bellow coding
几个小时后我也遇到了同样的问题,我使用 JSON.parse(data) 修复了它。就像波纹管编码
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
你是直接传递数据。所以只有你得到 [objcet Object]...如果你使用 JSON.parse(data) ,你会得到每个项目的 id
回答by Shwetha
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.
console.dir()
将使您打印对象内的属性。
回答by Emil Haugberg van Veen
You can use JSON.stringify()
.
您可以使用JSON.stringify()
.
The data needs to have a JSON structure however.
但是,数据需要具有 JSON 结构。
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()