Javascript 如何在JSON对象中循环使用$。每个JQuery?

Javascript 如何在JSON对象中循环使用$。每个JQuery?,javascript,jquery,json,Javascript,Jquery,Json,我想使用来自的JSON数据进行实时搜索。JSON包含一些对象,我想获取的对象是结果,它是一个数组,包含属性名称,高度,质量,等等的对象 JSON的片段如下所示: { "count": 87, "next": "http://swapi.co/api/people/?format=json&page=2", "previous": null, "results": [ { "name": "Luke Skywalker", "

我想使用来自的JSON数据进行实时搜索。JSON包含一些
对象
,我想获取的
对象是
结果
,它是一个
数组
,包含属性
名称
高度
质量
,等等的
对象

JSON的片段如下所示:

    {
    "count": 87,
    "next": "http://swapi.co/api/people/?format=json&page=2",
    "previous": null,
    "results": [
    {
    "name": "Luke Skywalker",
    "height": "172",
    "mass": "77",
    "hair_color": "blond",
    "skin_color": "fair",
    "eye_color": "blue",
    "birth_year": "19BBY",
    "gender": "male",
    "homeworld": "http://swapi.co/api/planets/1/",
    "films": [
    "http://swapi.co/api/films/6/",
    "http://swapi.co/api/films/3/",
    "http://swapi.co/api/films/2/",
    "http://swapi.co/api/films/1/",
    "http://swapi.co/api/films/7/"
    ],
    "species": [
    "http://swapi.co/api/species/1/"
    ],
    "vehicles": [
    "http://swapi.co/api/vehicles/14/",
    "http://swapi.co/api/vehicles/30/"
    ],
    "starships": [
    "http://swapi.co/api/starships/12/",
    "http://swapi.co/api/starships/22/"
    ],
    "created": "2014-12-09T13:50:51.644000Z",
    "edited": "2014-12-20T21:17:56.891000Z",
    "url": "http://swapi.co/api/people/1/"
    }
]
}
$.getJSON('http://swapi.co/api/people/?format=json', function(data) {
  $.each(data.results, function(key, value){
    console.log("key", key); 
    console.log("value", value.name)
   })
})
我尝试使用此代码,但控制台显示
未捕获类型错误:无法读取未定义的属性“name”(第7行)

$(“#搜索”).keyup(函数(){
var searchField=$('#search').val();
var myExp=新的RegExp(searchField,“i”);
$.getJSON('http://swapi.co/api/people/?format=json,函数(数据){
var输出='
    ; $。每个(数据、函数(键、值){ if((val.results.name.search(myExp)!=-1)|| (val.results.height.search(myExp)!=-1)){ 输出+='
  • '; 输出+=''+val.name+''; 输出+=''+val.height+'

    '; 输出+='
  • '; } }); 输出+='
'; $('#update').html(输出); }); });

我知道我的循环有问题,我试着寻找答案,但我不能得到正确的答案。你能帮帮我吗?

我想你应该有这样的东西:

output += '<h2>'+ val.results.name +'</h2>';
output += '<p>'+ val.results.height +'</p>';
output+=''+val.results.name+'';
输出+=''+val.results.height+'

';

正如您在
if
条件中所做的那样

我认为您应该有如下情况:

output += '<h2>'+ val.results.name +'</h2>';
output += '<p>'+ val.results.height +'</p>';
output+=''+val.results.name+'';
输出+=''+val.results.height+'

';

正如您在
中所做的那样,如果
条件

是一个简单的错误,您应该迭代data.results

$.getJSON('http://swapi.co/api/people/?format=json', function(data) {
    $.each(data.results, function(key, val) { 
    console.log(val.name); 
    })
});
您的JSON包含:

  • “计数”:87
  • “下一个”:“
  • “先前”:空
  • “结果”:您想要的数据

因此,您必须获得结果并对其进行迭代。

这是一个简单的错误,您应该对数据进行迭代。结果

$.getJSON('http://swapi.co/api/people/?format=json', function(data) {
    $.each(data.results, function(key, val) { 
    console.log(val.name); 
    })
});
您的JSON包含:

  • “计数”:87
  • “下一个”:“
  • “先前”:空
  • “结果”:您想要的数据

因此,您必须获得结果并对其进行迭代。

使您的
$。每个
循环如下:因为
val.results
是json的数组格式

$.each(data, function(key, val) {
        var results = val.results;
        $.each(results,function(index,value){
            if ((value[index].name.search(myExp) != -1) ||
            (value[index].height.search(myExp) != -1)) {
                output += '<li>';
                output += '<h2>'+ value[index].name +'</h2>';
                output += '<p>'+ value[index].height +'</p>';
                output += '</li>';
            }
        });

        });
$。每个(数据、函数(键、值){
var结果=val.results;
$.each(结果、函数(索引、值){
if((值[index].name.search(myExp)!=-1)||
(值[index].height.search(myExp)!=-1)){
输出+='
  • '; 输出+=''+值[索引]。名称+''; 输出+=''+值[索引]。高度+'

    '; 输出+='
  • '; } }); });
    希望它能正常工作

    或者干脆

            $.each(data.results,function(index,value){
                if ((value[index].name.search(myExp) != -1) ||
                (value[index].height.search(myExp) != -1)) {
                    output += '<li>';
                    output += '<h2>'+ value[index].name +'</h2>';
                    output += '<p>'+ value[index].height +'</p>';
                    output += '</li>';
                }
            });
    
    $。每个(数据、结果、函数(索引、值){
    if((值[index].name.search(myExp)!=-1)||
    (值[index].height.search(myExp)!=-1)){
    输出+='
  • '; 输出+=''+值[索引]。名称+''; 输出+=''+值[索引]。高度+'

    '; 输出+='
  • '; } });

    可能就足够了。

    让您的
    $。每个
    循环如下:因为
    val.results
    是json的数组格式

    $.each(data, function(key, val) {
            var results = val.results;
            $.each(results,function(index,value){
                if ((value[index].name.search(myExp) != -1) ||
                (value[index].height.search(myExp) != -1)) {
                    output += '<li>';
                    output += '<h2>'+ value[index].name +'</h2>';
                    output += '<p>'+ value[index].height +'</p>';
                    output += '</li>';
                }
            });
    
            });
    
    $。每个(数据、函数(键、值){
    var结果=val.results;
    $.each(结果、函数(索引、值){
    if((值[index].name.search(myExp)!=-1)||
    (值[index].height.search(myExp)!=-1)){
    输出+='
  • '; 输出+=''+值[索引]。名称+''; 输出+=''+值[索引]。高度+'

    '; 输出+='
  • '; } }); });
    希望它能正常工作

    或者干脆

            $.each(data.results,function(index,value){
                if ((value[index].name.search(myExp) != -1) ||
                (value[index].height.search(myExp) != -1)) {
                    output += '<li>';
                    output += '<h2>'+ value[index].name +'</h2>';
                    output += '<p>'+ value[index].height +'</p>';
                    output += '</li>';
                }
            });
    
    $。每个(数据、结果、函数(索引、值){
    if((值[index].name.search(myExp)!=-1)||
    (值[index].height.search(myExp)!=-1)){
    输出+='
  • '; 输出+=''+值[索引]。名称+''; 输出+=''+值[索引]。高度+'

    '; 输出+='
  • '; } });

    对你来说可能足够了。

    试试这个

    var a = //your json;
    
    $.each(a,function(i,val){
    if(val.results.length != 0){
    $.each(val.results,function(k,value){
    console.log(value.name)})
    }
    })
    

    试试这个

    var a = //your json;
    
    $.each(a,function(i,val){
    if(val.results.length != 0){
    $.each(val.results,function(k,value){
    console.log(value.name)})
    }
    })
    

    您需要像这样迭代
    数据
    对象的
    结果

        {
        "count": 87,
        "next": "http://swapi.co/api/people/?format=json&page=2",
        "previous": null,
        "results": [
        {
        "name": "Luke Skywalker",
        "height": "172",
        "mass": "77",
        "hair_color": "blond",
        "skin_color": "fair",
        "eye_color": "blue",
        "birth_year": "19BBY",
        "gender": "male",
        "homeworld": "http://swapi.co/api/planets/1/",
        "films": [
        "http://swapi.co/api/films/6/",
        "http://swapi.co/api/films/3/",
        "http://swapi.co/api/films/2/",
        "http://swapi.co/api/films/1/",
        "http://swapi.co/api/films/7/"
        ],
        "species": [
        "http://swapi.co/api/species/1/"
        ],
        "vehicles": [
        "http://swapi.co/api/vehicles/14/",
        "http://swapi.co/api/vehicles/30/"
        ],
        "starships": [
        "http://swapi.co/api/starships/12/",
        "http://swapi.co/api/starships/22/"
        ],
        "created": "2014-12-09T13:50:51.644000Z",
        "edited": "2014-12-20T21:17:56.891000Z",
        "url": "http://swapi.co/api/people/1/"
        }
    ]
    }
    
    $.getJSON('http://swapi.co/api/people/?format=json', function(data) {
      $.each(data.results, function(key, value){
        console.log("key", key); 
        console.log("value", value.name)
       })
    })
    
    或者在您的情况下:

    $('#search').keyup(function() {
        var searchField = $('#search').val();
        var myExp = new RegExp(searchField, "i");
        $.getJSON('http://swapi.co/api/people/?format=json', function(data) {
            var output = '<ul class="searchresults">';
            $.each(data.results, function(key, val) { //Only this line changes.
                if ((val.results.name.search(myExp) != -1) ||
                (val.results.height.search(myExp) != -1)) {
                    output += '<li>';
                    output += '<h2>'+ val.name +'</h2>';
                    output += '<p>'+ val.height +'</p>';
                    output += '</li>';
                }
            });
            output += '</ul>';
            $('#update').html(output);
        });
    });
    
    $(“#搜索”).keyup(函数(){
    var searchField=$('#search').val();
    var myExp=新的RegExp(searchField,“i”);
    $.getJSON('http://swapi.co/api/people/?format=json,函数(数据){
    var输出='
      ; $.each(data.results,function(key,val){//只有这一行更改。 if((val.results.name.search(myExp)!=-1)|| (val.results.height.search(myExp)!=-1)){ 输出+='
    • '; 输出+=''+val.name+''; 输出+=''+val.height+'

      '; 输出+='
    • '; } }); 输出+='
    '; $('#update').html(输出); }); });
    您需要迭代
    数据
    对象的
    结果,如下所示:

        {
        "count": 87,
        "next": "http://swapi.co/api/people/?format=json&page=2",
        "previous": null,
        "results": [
        {
        "name": "Luke Skywalker",
        "height": "172",
        "mass": "77",
        "hair_color": "blond",
        "skin_color": "fair",
        "eye_color": "blue",
        "birth_year": "19BBY",
        "gender": "male",
        "homeworld": "http://swapi.co/api/planets/1/",
        "films": [
        "http://swapi.co/api/films/6/",
        "http://swapi.co/api/films/3/",
        "http://swapi.co/api/films/2/",
        "http://swapi.co/api/films/1/",
        "http://swapi.co/api/films/7/"
        ],
        "species": [
        "http://swapi.co/api/species/1/"
        ],
        "vehicles": [
        "http://swapi.co/api/vehicles/14/",
        "http://swapi.co/api/vehicles/30/"
        ],
        "starships": [
        "http://swapi.co/api/starships/12/",
        "http://swapi.co/api/starships/22/"
        ],
        "created": "2014-12-09T13:50:51.644000Z",
        "edited": "2014-12-20T21:17:56.891000Z",
        "url": "http://swapi.co/api/people/1/"
        }
    ]
    }
    
    $.getJSON('http://swapi.co/api/people/?format=json', function(data) {
      $.each(data.results, function(key, value){
        console.log("key", key); 
        console.log("value", value.name)
       })
    })
    
    或者在您的情况下:

    $('#search').keyup(function() {
        var searchField = $('#search').val();
        var myExp = new RegExp(searchField, "i");
        $.getJSON('http://swapi.co/api/people/?format=json', function(data) {
            var output = '<ul class="searchresults">';
            $.each(data.results, function(key, val) { //Only this line changes.
                if ((val.results.name.search(myExp) != -1) ||
                (val.results.height.search(myExp) != -1)) {
                    output += '<li>';
                    output += '<h2>'+ val.name +'</h2>';
                    output += '<p>'+ val.height +'</p>';
                    output += '</li>';
                }
            });
            output += '</ul>';
            $('#update').html(output);
        });
    });
    
    $(“#搜索”).keyup(函数(){
    var searchField=$('#search').val();
    var myExp=新的RegExp(searchField,“i”);
    $.getJSON('http://swapi.co/api/people/?format=json,函数(数据){
    var输出='
      ; $.each(data.results,function(key,val){//只有这一行更改。 if((val.results.name.search(myExp)!=-1)|| (val.results.height.search(myExp)!=-1)){ 输出+='
    • '; 输出+=''+val.name+