jQuery 如何打印一个json对象数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8363434/
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 print a json object array
提问by Nils_e
I found a jquery function online that i then tweaked a little, but it takes a youtube channels feed data and turns it into a json object. It then stores data from the objects arrays. I need to store extra data that IS given, but I dont know how to display the data so I can see the data in its arrays. Im sure this sounds confusing and that is because I dont know about json objects or how to print out the data in jquery. Ive done it before in php with the print function I believe.
我在网上找到了一个 jquery 函数,然后我稍微调整了一下,但它需要一个 youtube 频道提要数据并将其转换为 json 对象。然后它存储来自对象数组的数据。我需要存储给出的额外数据,但我不知道如何显示数据,以便我可以在其数组中查看数据。我确定这听起来令人困惑,那是因为我不知道 json 对象或如何在 jquery 中打印出数据。我以前用我相信的打印功能在 php 中完成了它。
This is the function mentioned:
这是提到的功能:
$j(function() {
$j.getJSON('http://gdata.youtube.com/feeds/users/iCallOfDutyFILMS/uploads?orderby=published&alt=json-in-script&callback=?&max-results=1', function(data) {
$j.each(data.feed.entry, function(i, item) {
var updated = item.updated;
var url = item['media$group']['media$content'][0]['url'];
var thumb = item['media$group']['media$thumbnail'][0]['url'];
var numViews = item['yt$statistics']['viewCount'];
//var vidDescription = item[''][''];
var width = 710; //only change this number
var height = width*9/16;
//...rest left out...
});
});
});
I want to print out the array to see how to grab the video description. I just dont know the path to it through the array. Is there a way in jquery to print out the arrays similar to how I was able to in php?
我想打印出数组以查看如何获取视频描述。我只是不知道它通过数组的路径。在 jquery 中有没有办法打印出类似于我在 php 中能够打印的数组?
采纳答案by Anil
In Chrome or Firefox, Press F12
, This will bring up the Inspector. Click on 'Console' to show the console window.
在 Chrome 或 Firefox 中,按F12
,这将调出检查器。单击“控制台”以显示控制台窗口。
Now in your javascript code, whatever variable or object you want to print or 'inspect'. Pass it into the console log method.
现在在您的 javascript 代码中,您想要打印或“检查”的任何变量或对象。将其传递到控制台日志方法中。
var your_array_variable = "a string variable";
console.log(your_array_variable); //<-- This is what you want to do!
Now refresh the page and run the event or function that you are building.
现在刷新页面并运行您正在构建的事件或函数。
Voila!... In your console window you should see >Object
(If you print a Object).
瞧!...在您的控制台窗口中,您应该看到>Object
(如果您打印一个对象)。
Or in the example above.. You would see a string variable
in the console.
或者在上面的例子中......你会a string variable
在控制台中看到。
Click on the expand tree node, (>
) to view the object/variables details.
单击展开树节点 ( >
) 以查看对象/变量详细信息。
You can console.log almost anything!.. Its a great way to learn whats going on.
您几乎可以使用 console.log 记录任何内容!.. 这是了解正在发生的事情的好方法。
You can also set 'breakpoints', but this is a whole other question!..
您还可以设置“断点”,但这是另一个问题!..
I think what your looking for is:
我想你要找的是:
console.log(data); // To show the JSON data received
console.log(data); // To show the JSON data received
Update
Try this, It worked for me, in console you will see 2 objects you can click on and inspect. The first is the data from the json request, the second is the item looped in the $.each.
This worked for me, On click of anything with the id="get_json"
. This will make the request, and log the response to the console.
更新
试试这个,它对我有用,在控制台中,您将看到 2 个可以单击并检查的对象。第一个是来自 json 请求的数据,第二个是 $.each 中循环的项目。这对我有用,点击任何带有id="get_json"
. 这将发出请求,并将响应记录到控制台。
$('#get_json').click(function(e){
e.preventDefault();
$(function() {
var json_url = 'http://gdata.youtube.com/feeds/users/iCallOfDutyFILMS/uploads?orderby=published&alt=json-in-script&max-results=1&callback=?';
$.getJSON(json_url, function(data) {
console.log(data); // Here we log to our console
$.each(data.feed.entry, function(i, item) {
console.log(item); // This is what you want i think
var updated = item.updated;
var url = item['media$group']['media$content'][0]['url'];
var thumb = item['media$group']['media$thumbnail'][0]['url'];
var numViews = item['yt$statistics']['viewCount'];
//var vidDescription = item[''][''];
var width = 710; //only change this number
var height = width*9/16;
//...rest left out...
});
}).success(function() { alert("success"); })
// Errors are your friend!.. Use them..
.error(function(jqXHR, textStatus, errorThrown) {
console.log("error " + textStatus);
console.log("error throw " + errorThrown);
console.log("incoming Text " + jqXHR.responseText);
}) // End of .error
.complete(function() { alert("complete"); });
});
});
回答by Jemaclus
If you use Firebug or other developer tools, you can console.log(data) and then inspect the JSON response in Firebug (or whatever inspection tool you're using). Then you can use that view to determine the "path" to access the video description that you want.
如果您使用 Firebug 或其他开发人员工具,您可以使用 console.log(data) 然后在 Firebug(或您使用的任何检查工具)中检查 JSON 响应。然后,您可以使用该视图来确定访问所需视频描述的“路径”。
Good luck.
祝你好运。
回答by aziz punjani
Do a console.log( yourarray );
You will be able to see the output in the browsers JavaScript console.
执行 aconsole.log( yourarray );
您将能够在浏览器的 JavaScript 控制台中看到输出。
回答by JesseBuesking
Why not look at the object using the inspector built into your browser? Chrome, Firefox, and newer versions of IE have inspectors that could help you without the need to write any code of your own.
为什么不使用浏览器内置的检查器查看对象呢?Chrome、Firefox 和较新版本的 IE 都有检查器,它们可以帮助您,而无需您自己编写任何代码。
回答by Windsinger
There is also the command console.dir( object )
which will specifically give you a better dump to explore using a tree-system in FireFox and Chrome.
还有一个命令console.dir( object )
可以专门为您提供更好的转储,以便在 FireFox 和 Chrome 中使用树系统进行探索。
More information: https://developer.mozilla.org/en-US/docs/Web/API/console.dir
更多信息:https: //developer.mozilla.org/en-US/docs/Web/API/console.dir
回答by palswim
If your browser or environment has the right JSON
object (e.g. Firefox), you can use JSON.stringify(obj)
to convert the object to a string and use however you wish.
如果您的浏览器或环境具有正确的JSON
对象(例如 Firefox),您可以使用JSON.stringify(obj)
将对象转换为字符串并随意使用。
回答by amit_g
If you want to inspect the JSON that you are getting from third party before writing any of your code against it, go to http://jsonlint.com/and enter the Url (plain without callback) and click Validate. It would validate and display the formatted JSON.
如果您想在编写任何代码之前检查从第三方获得的 JSON,请转到http://jsonlint.com/并输入 Url(无回调的纯文本)并单击验证。它将验证并显示格式化的 JSON。