使用 Javascript 输出 Json 数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13658523/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 12:36:23  来源:igfitidea点击:

Output Json Arrays with Javascript

javascriptarraysjsonjquery

提问by RaveN

I am really new to Javascript and Json and need help with the following. This is some json data:

我对 Javascript 和 Json 真的很陌生,需要以下方面的帮助。这是一些json数据:

"genres": [

 {
    "id": 28,
    "name": "Action"
 },
 {
    "id": 18,
    "name": "Drama"
 },
 {
    "id": 14,
    "name": "Fantasy"
 },
 {
    "id": 36,
    "name": "History"
 }

],

Now I would like to output all names of genres

现在我想输出所有类型的名称

I'm working with jquery mobile and ajax to retrieve json data. Inside my function it looks sth like this:

我正在使用 jquery mobile 和 ajax 来检索 json 数据。在我的函数中,它看起来像这样:

var genres = results.genres[0].name;
$('#tmdbInfo2').html('<br>Genres: ' +genres);

In this example the first element of genres will we shown (Action), but not all. The output should be: Action, Drama, Fantasy, History. Sometimes the number of elements of genres varies..

在这个例子中,我们将展示流派的第一个元素(动作),但不是全部。输出应该是:动作、戏剧、幻想、历史。有时流派的元素数量会有所不同。

Please help me, its very confusing for me O_o

请帮帮我,这让我很困惑O_o

采纳答案by Eric

Another way to iterate:

另一种迭代方式:

results.genres.forEach(function(genre) {
    console.log(genre.name);
});

For what you're trying to do:

对于您正在尝试做的事情:

$('#tmdbInfo2').html(
    'Genres: ' + results.genres.map(function(genre) {
        return genre.name;
    }).join(', ')
);

回答by kapa

Plain Javascript

纯Javascript

This is how you iterate on the elements of an array, using a forloop (do not use a for...inloop on arrays, if you ever get tempted).

这就是您使用循环迭代数组元素的方式for(如果您受到诱惑,请不要for...in在数组上使用循环)。

for (var i = 0; i < results.genres.length; i++) {
    console.log( results.genres[i].name );
}

Your array is called results.genreshere. Inside the loop, results.genres[i]will always refer to the current element.

您的数组results.genres在此处被调用。在循环内部,results.genres[i]将始终引用当前元素。

jQuery

jQuery

I see that you are using jQuery, so you can also use $.each()like this:

我看到你在使用 jQuery,所以你也可以$.each()这样使用:

$.each(results.genres, function (currentIndex, currentElem) {
    console.log( currentElem.name );
});

回答by TheLastCodeBender

You can access array values by using a for-in loop like this:

您可以使用像这样的 for-in 循环来访问数组值:

var result = "";

for (i in genres.name) {
  result += genres.name[i];
} 

console.log(result);

回答by Rogerio de Moraes

You can use

您可以使用

<script>
   results = '{ "genres": [{ "id": 28, "name": "Action" },{ "id": 18, "name": "Drama" },{ "id": 14, "name": "Fantasy" },{ "id": 36, "name": "History" }] }';    
   results = JSON.parse(results);

       for($i in results.genres){
            console.log(results.genres[$i].name);
       }

</script>

JSON.parsemethod parses a string as JSON, optionally transforming the value produced by parsing and you can get value from node like array. When you use for($i in results.genres)you dont net set limite size, 'case for get all array/object elements.

JSON.parse方法将字符串解析为 JSON,可以选择转换解析生成的值,您可以从像数组这样的节点获取值。当您使用时,您for($i in results.genres)不会净设置限制大小,'获取所有数组/对象元素的情况。

Group Elementsneed are between {"genres": [{ ...}] }for your JSON'll be read.

组元素需要在{"genres": [{ ...}] } 之间,以便读取您的 JSON。