Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
170 views
in Technique[技术] by (71.8m points)

javascript - 如何在javascript函数中传递一组json数据?(How to pass a set of json data in javascript function?)

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){alert(data);) } add(data);(} add(数据);) I only get [object Object],[object Object] ... What's the problem here?(我只得到[object Object],[object Object] ...这是什么问题?) Thanks.(谢谢。)   ask by D Kim translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The JSON string is wrong.(JSON字符串错误。)

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字符串转换为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.(该alert将为您提供[object Object]输出,因为变量data本身就是对象。) So if you want to see the whole json data, you need to console.log as:(因此,如果您想查看整个json数据,则需要console.log为:) console.log(JSON.parse(d)); Watch the demo .(观看演示 。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...