最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Output Json Arrays with Javascript - Stack Overflow

programmeradmin3浏览0评论

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

"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:

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

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

"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:

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

Share Improve this question edited Dec 1, 2012 at 10:59 kapa 78.7k21 gold badges164 silver badges177 bronze badges asked Dec 1, 2012 at 10:31 RaveNRaveN 1151 gold badge1 silver badge7 bronze badges 3
  • 2 I am sorry to say that, but you should have simply googled that. Or open up a basic tutorial on Javascript arrays. – kapa Commented Dec 1, 2012 at 10:33
  • I already did, believe me. I'm not a programmer. I found many examples, but couldn't transfer this to my problem. – RaveN Commented Dec 1, 2012 at 10:35
  • 2 Nobody is born as a programmer. If you want to do programming, well, you have no other chance but to learn it. – kapa Commented Dec 1, 2012 at 10:46
Add a comment  | 

4 Answers 4

Reset to default 8

Plain 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 (var i = 0; i < results.genres.length; i++) {
    console.log( results.genres[i].name );
}

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

jQuery

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

$.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:

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. When you use for($i in results.genres) you dont net set limite size, 'case for get all array/object elements.

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

发布评论

评论列表(0)

  1. 暂无评论