【问题标题】:Only show JSon by key value仅按键值显示 JSON
【发布时间】:2017-11-24 10:25:54
【问题描述】:

我被一个 id 渲染的 JSON 数据卡住了,已经浏览了几篇文章但没有工作。

我有 ajax 调用:

$.ajax({
    url: '../../../get/api/city.json',
    dataType: 'json',
})
.done(function(data){
    console.log(data);
    var HTML = ''; 
        $.each(data.city, function(i,city){
        HTML += '<span>'+city.city_name+'</span>'; });

       $('#title').append(HTML);
    })
.fail(function(){
    alert('failed');
});

而json输出为:

{"city":[
{"id":"4","year":"2014","city_name":"City A"},
{"id":"5","year":"2014","city_name":"City B"},
{"id":"6","year":"2014","city_name":"City C"}]}

我的情况我创建了一个html页面来呈现城市A的信息,我想要的是过滤上面的JSON数据只呈现来自城市A的信息。

非常感谢你们的任何帮助。 谢谢

【问题讨论】:

  • 您是否搜索过“如何访问 json 数据”或者您是否跳过了该内容并直接来这里让其他人为您做?这是非常基本的......你会在第一个结果中得到答案。
  • 是的,我之前尝试过搜索,所有这些都显示了如何通过行号 data[0].blabla 获取数据

标签: javascript html json ajax


【解决方案1】:

使用 Array 的 filter() 尝试以下操作:

var info = {"city":[
{"id":"4","year":"2014","city_name":"City A"},
{"id":"5","year":"2014","city_name":"City B"},
{"id":"6","year":"2014","city_name":"City C"}]};

var res = info.city.filter(function(item){
  return item.city_name =='City A';
});
console.log(res);
var HTML = '<span>'+res[0].city_name+'</span>';
$('#title').append(HTML)
HTML = '<span>'+res[0].id+'</span>';
$('#id').append(HTML);;
HTML = '<span>'+res[0].year+'</span>';
$('#year').append(HTML);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="title">Title: </div>
<div id="id">Id: </div>
<div id="year">Year: </div>

更新: 一旦您的请求服务器发送数据,您可以使用以下代码:

$.ajax({
  url: 'http://207.254.40.122/deetab/json.json',
  dataType: 'json',
  crossDomain: true
}).done(function(data){
    console.log(data);// Check here whether sever send data or not
    var res = data.city.filter(function(item){
       return item.city_name =='City A';
    });
   //console.log(res);
   var HTML = '<span>'+res[0].city_name+'</span>';
   $('#title').append(HTML)
   HTML = '<span>'+res[0].id+'</span>';
   $('#id').append(HTML);;
   HTML = '<span>'+res[0].year+'</span>';
   $('#year').append(HTML);
});

【讨论】:

  • 目前是基于城市的。如果你想基于 id 那么你需要在 filter 函数中改变它......谢谢。
  • 嗨,试过这个代码。但仍然没有显示(试图将使用的 url 替换为 var info 中的 json 数据。您能否给我一个示例,如何在我当前的代码上实现这一点,或者从 url 获取数据?因为这是来自 sql 的动态数据
  • @da_root,我提供了基于 JSON 输出的解决方案。你的console.log(data);done() 里面显示了什么?我相信,如果您将我的代码放在您的 ajax done() 中,代码将会正常工作。虽然如果你提供一个带有 sql 数据的 jsfiddle 会更有效.....谢谢。
  • 嗨,你能在这里检查一下小提琴吗:jsfiddle.net/oL1jf9bv
  • ajax 请求中没有数据。这里的问题是Failed to load http://207.254.40.122/deetab/json.json: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. 当您请求的数据在另一个域中时,就会发生这种情况。有关详细信息,请参阅此链接:stackoverflow.com/questions/20035101/…
【解决方案2】:

您可以在$.each 中添加一个过滤器,以检查城市 ID 是否是您想要的。

$.each(data.city, function (i, city) {
    if (city.id === 4)
        HTML += '<span>' + city.city_name + '</span>'; 
});

【讨论】:

  • 谢谢,试过了,但仍然显示全名:(
  • 这不是你想要的吗?
  • 我的意思是这仍然显示城市名称的整个数据
  • 如何在多个标签上使用它?我尝试添加另一个: HTML += '' + city.year+ '';但年份值未过滤
  • 您是否在 if 语句中添加了 {}?因为现在它只会在语句之后的第一行起作用。
【解决方案3】:

可以使用您想要的城市名称准备一个过滤器并将其应用于您的数据数组以过滤您喜欢的数据,例如:

var cities = {
  "city": [{
    "id": "4",
    "year": "2014",
    "city_name": "City A"
  }, {
    "id": "5",
    "year": "2014",
    "city_name": "City B"
  }, {
    "id": "6",
    "year": "2014",
    "city_name": "City C"
  }]
};

//Want only city with this name
var filter = "City A";

console.log(cities.city);

cities.city = cities.city.filter(function(obj) {
  return obj.city_name == filter;
});

console.log(cities.city);

【讨论】:

  • 如何创建数组数据,因为我的 json 是从 php 生成的,对此我真的很陌生。你能指导我如何输入我上面的代码吗? :( 或者我应该编辑生成 json 的 php?
  • 从 php 生成的 JSON 可能被视为字符串以使用 JSON.parse() 函数对其进行转换。
猜你喜欢
  • 2016-01-17
  • 2021-03-03
  • 1970-01-01
  • 2021-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多