迭代JSON响应以从数组/对象获取键和值

时间:2022-09-13 13:20:42

I'm trying to iterate through the list of industryIdentifiers and save the type and identifier values.

我正在尝试遍历industryIdentifier列表并保存类型和标识符值。

I've tried using the following code, but am getting an undefined as the output.

我已经尝试使用以下代码,但我得到一个未定义的输出。

var url = "https://www.googleapis.com/books/v1/volumes/EXiMZwEACAAJ";
        $.getJSON(url, function(data) {
                var bookID = data.items[0].id;
                var title = data.items[0].volumeInfo.title;
                var industryIdentifiers = data.items[0].volumeInfo.industryIdentifiers;
                $.each(industryIdentifiers, function(result){
                    var idType = result.type;
                    var idValue = result.identifier;
                    alert (idType + " = " + idValue);
                });
        });

3 个解决方案

#1


1  

It is because you are wrongly using $.each, $.each has following callback structure ,

这是因为你错误地使用$ .each,$ .each有以下回调结构,

callback

Type: Function( String propertyName, Object valueOfProperty )
The function that will be executed on every object.

So, simply using ,

所以,简单地使用,

 $.each(industryIdentifiers, function(result){

will produce result as index instead of your sought out value. Thus giving you the undefined values.

将产生结果作为指数而不是您所寻求的价值。从而为您提供未定义的值。

Here is the working code,

这是工作代码,

var url = "https://www.googleapis.com/books/v1/volumes/EXiMZwEACAAJ";
        $.getJSON(url, function(data) {
                var bookID = data.id;
                var title = data.volumeInfo.title;
                var industryIdentifiers =       data.volumeInfo.industryIdentifiers;
                $.each(industryIdentifiers, function(index,value){
                    var idType = value.type;
                    var idValue = value.identifier;
                    alert (idType + " = " + idValue);
                });
        });

And, you can view the demo here.

并且,您可以在此处查看演示。

And , also you are wrongly accessing the result JSON. Since it is returning a single value you can directly access it as data.id.

而且,您错误地访问结果JSON。由于它返回单个值,您可以直接将其作为data.id访问。

#2


0  

Try this code...

试试这个代码......

 $(document).ready( function() {
  var url = "https://www.googleapis.com/books/v1/volumes/EXiMZwEACAAJ";
        $.getJSON(url).done(function( data ){
                var bookID = data.id;
                var title = data.volumeInfo.title;
                var industryIdentifiers = data.volumeInfo.industryIdentifiers;
                //console.log(industryIdentifiers);
                $.each(industryIdentifiers, function(i, item){
                    console.log(item);
                    var idType = item.type;
                    var idValue = item.identifier;
                    alert (idType + " = " + idValue);
                });
        });
 });

Fiddle: http://jsfiddle.net/v5cornfb/

#3


0  

You have two problems. The first issue is that data.items is undefined, for accessing the properties in that data object you have to do this data.nameOfProperty. Your second issue is that when you are iterating through the industryIdentifiers you have to declare a second argument to the anonymous function in the $.each call. Since the first argument is only the index of the array and the second argument is the value of the element, in this case it would be the object that has the properties of type and identifier.

你有两个问题。第一个问题是data.items未定义,为了访问该数据对象中的属性,您必须执行此data.nameOfProperty。您的第二个问题是,当您遍历industryIdentifier时,您必须在$ .each调用中为匿名函数声明第二个参数。由于第一个参数只是数组的索引而第二个参数是元素的值,因此在这种情况下,它将是具有类型和标识符属性的对象。

#1


1  

It is because you are wrongly using $.each, $.each has following callback structure ,

这是因为你错误地使用$ .each,$ .each有以下回调结构,

callback

Type: Function( String propertyName, Object valueOfProperty )
The function that will be executed on every object.

So, simply using ,

所以,简单地使用,

 $.each(industryIdentifiers, function(result){

will produce result as index instead of your sought out value. Thus giving you the undefined values.

将产生结果作为指数而不是您所寻求的价值。从而为您提供未定义的值。

Here is the working code,

这是工作代码,

var url = "https://www.googleapis.com/books/v1/volumes/EXiMZwEACAAJ";
        $.getJSON(url, function(data) {
                var bookID = data.id;
                var title = data.volumeInfo.title;
                var industryIdentifiers =       data.volumeInfo.industryIdentifiers;
                $.each(industryIdentifiers, function(index,value){
                    var idType = value.type;
                    var idValue = value.identifier;
                    alert (idType + " = " + idValue);
                });
        });

And, you can view the demo here.

并且,您可以在此处查看演示。

And , also you are wrongly accessing the result JSON. Since it is returning a single value you can directly access it as data.id.

而且,您错误地访问结果JSON。由于它返回单个值,您可以直接将其作为data.id访问。

#2


0  

Try this code...

试试这个代码......

 $(document).ready( function() {
  var url = "https://www.googleapis.com/books/v1/volumes/EXiMZwEACAAJ";
        $.getJSON(url).done(function( data ){
                var bookID = data.id;
                var title = data.volumeInfo.title;
                var industryIdentifiers = data.volumeInfo.industryIdentifiers;
                //console.log(industryIdentifiers);
                $.each(industryIdentifiers, function(i, item){
                    console.log(item);
                    var idType = item.type;
                    var idValue = item.identifier;
                    alert (idType + " = " + idValue);
                });
        });
 });

Fiddle: http://jsfiddle.net/v5cornfb/

#3


0  

You have two problems. The first issue is that data.items is undefined, for accessing the properties in that data object you have to do this data.nameOfProperty. Your second issue is that when you are iterating through the industryIdentifiers you have to declare a second argument to the anonymous function in the $.each call. Since the first argument is only the index of the array and the second argument is the value of the element, in this case it would be the object that has the properties of type and identifier.

你有两个问题。第一个问题是data.items未定义,为了访问该数据对象中的属性,您必须执行此data.nameOfProperty。您的第二个问题是,当您遍历industryIdentifier时,您必须在$ .each调用中为匿名函数声明第二个参数。由于第一个参数只是数组的索引而第二个参数是元素的值,因此在这种情况下,它将是具有类型和标识符属性的对象。