简体   繁体   English

用Javascript输出Json数组

[英]Output Json Arrays with Javascript

I am really new to Javascript and Json and need help with the following. 我是Javascript和Json的新手,需要以下帮助。 This is some json data: 这是一些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. 我正在使用jquery mobile和ajax来检索json数据。 Inside my function it looks sth like this: 在我的功能里面看起来像这样:

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. 在这个例子中,我们将展示类型的第一个元素(Action),但不是全部。 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

Plain Javascript 简单的Javascript

This is how you iterate on the elements of an array, using a for loop ( do not use a for...in loop 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.genres here. 您的数组在此处称为results.genres Inside the loop, results.genres[i] will always refer to the current element. 在循环内部, 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 );
});

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(', ')
);

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);

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.parse method parses a string as JSON, optionally transforming the value produced by parsing and you can get value from node like array. JSON.parse方法将字符串解析为JSON,可选地转换解析产生的值,您可以从类似数组的节点获取值。 When you use for($i in results.genres) you dont net set limite size, 'case for get all array/object elements. 当你使用for($i in results.genres)你不会设置有限大小,'获取所有数组/对象元素的情况。

Group Elements need are between { "genres": [{ ...}] } for your JSON'll be read. 组元素需要在{ “genres”:[{...}] }之间,因为您的JSON将被阅读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM