如何从JSON文件提取某些属性

时间:2019-03-20 01:35:32

标签: javascript html json

所以我有一个JSON文件:

{
    "business_id": "5UmKMjUEUNdYWqANhGckJw",
    "full_address": "4734 Lebanon Church Rd\nDravosburg, PA 15034",
    "hours": {
        "Friday": {
            "close": "21:00",
            "open": "11:00"
        },
        "Tuesday": {
            "close": "21:00",
            "open": "11:00"
        },
        "Thursday": {
            "close": "21:00",
            "open": "11:00"
        },
        "Wednesday": {
            "close": "21:00",
            "open": "11:00"
        },
        "Monday": {
            "close": "21:00",
            "open": "11:00"
        }
    },
    "open": true,
    "categories": ["Fast Food",
    "Restaurants"],
    "city": "Dravosburg",
    "review_count": 4,
    "name": "Mr Hoagie",
    "neighborhoods": [],
    "longitude": -79.9007057,
    "state": "PA",
    "stars": 4.5,
    "latitude": 40.3543266,
    "attributes": {
        "Take-out": true,
        "Drive-Thru": false,
        "Good For": {
            "dessert": false,
            "latenight": false,
            "lunch": false,
            "dinner": false,
            "brunch": false,
            "breakfast": false
        },
        "Caters": false,
        "Noise Level": "average",
        "Takes Reservations": false,
        "Delivery": false,
        "Ambience": {
            "romantic": false,
            "intimate": false,
            "classy": false,
            "hipster": false,
            "divey": false,
            "touristy": false,
            "trendy": false,
            "upscale": false,
            "casual": false
        },
        "Parking": {
            "garage": false,
            "street": false,
            "validated": false,
            "lot": false,
            "valet": false
        },
        "Has TV": false,
        "Outdoor Seating": false,
        "Attire": "casual",
        "Alcohol": "none",
        "Waiter Service": false,
        "Accepts Credit Cards": true,
        "Good for Kids": true,
        "Good For Groups": true,
        "Price Range": 1
    },
    "type": "business"
}

,我想提取所有值为true的属性,并将它们存储到HTML表格中。我对这个问题有些困惑,但这就是我到目前为止的情况

function saf(){
  loadJSON(function(response){
    var actual_JSON = JSON.parse(response);

    var takeout = actual_JSON.attributes;
    for(i in takeout){
      if(takeout[i]==true){
        var tr = document.createElement("tr");
        var td = document.createElement("td");
        var table = document.getElementById("service_table");
        var textnode = document.createTextNode(takeout[i]);
        td.appendChild(textnode);
        tr.appendChild(td);
        table.appendChild(tr);
      }
    }

  });
}

要完成此任务,我还需要什么?有没有办法让我遍历JSON对象的每个循环?我不确定从这里要去哪里,所以我正在寻找有关此方面的帮助。我应该将数据追加到表对象吗?

2 个答案:

答案 0 :(得分:1)

JSON是javascript对象表示法,其名称已被建议为javascript对象。有两种访问JSON的方法:用点表示法通常类似于类对象,而用括号表示法则类似于数组。因此,假设您想在星期五访问营业时间。你可以写

const myjson = JSON.parse(response).
for (const key in myjson) {
    if (myjson.hasOwnProperty(key)) {
        console.log(key + " -> " + myjson[key]);
    }
}

在您的功能中也是如此。

function saf(){
  loadJSON(function(response){
    const actual_JSON = JSON.parse(response);    

    for(i in takeout){
      if(takeout[i]===true){
        const tr = document.createElement("tr");
        const td = document.createElement("td");
        const table = document.getElementById("service_table");
        const textnode = document.createTextNode(takeout[i]);
        tr.appendChild(textnode);
        table.appendChild(tr);
      }
    }    
  });
}

答案 1 :(得分:0)

使用Object(keys)获取属性列表

使用typeof yourObject === 'object'检查属性是否本身就是对象

进行递归以从嵌套对象中提取属性

const obj = {
    "business_id": "5UmKMjUEUNdYWqANhGckJw",
    "full_address": "4734 Lebanon Church Rd\nDravosburg, PA 15034",
    "hours": {
        "Friday": {
            "close": "21:00",
            "open": "11:00"
        },
        "Tuesday": {
            "close": "21:00",
            "open": "11:00"
        },
        "Thursday": {
            "close": "21:00",
            "open": "11:00"
        },
        "Wednesday": {
            "close": "21:00",
            "open": "11:00"
        },
        "Monday": {
            "close": "21:00",
            "open": "11:00"
        }
    },
    "open": true,
    "categories": ["Fast Food",
    "Restaurants"],
    "city": "Dravosburg",
    "review_count": 4,
    "name": "Mr Hoagie",
    "neighborhoods": [],
    "longitude": -79.9007057,
    "state": "PA",
    "stars": 4.5,
    "latitude": 40.3543266,
    "attributes": {
        "Take-out": true,
        "Drive-Thru": false,
        "Good For": {
            "dessert": false,
            "latenight": false,
            "lunch": false,
            "dinner": false,
            "brunch": false,
            "breakfast": false
        },
        "Caters": false,
        "Noise Level": "average",
        "Takes Reservations": false,
        "Delivery": false,
        "Ambience": {
            "romantic": false,
            "intimate": false,
            "classy": false,
            "hipster": false,
            "divey": true,
            "touristy": false,
            "trendy": false,
            "upscale": false,
            "casual": false
        },
        "Parking": {
            "garage": false,
            "street": false,
            "validated": true,
            "lot": true,
            "valet": false
        },
        "Has TV": false,
        "Outdoor Seating": false,
        "Attire": "casual",
        "Alcohol": "none",
        "Waiter Service": false,
        "Accepts Credit Cards": true,
        "Good for Kids": true,
        "Good For Groups": true,
        "Price Range": 1
    },
    "type": "business"
};

const result = [];

const extract = (input) => Object.keys(input).forEach(key => {
  // If property is an object => recursion
  if (typeof input[key] === 'object') {
    extract(input[key]);
  }
  
  // If property is true, add it to the results
  if (input[key] === true) {
    result.push(key);
  }
});

// Execute
extract(obj.attributes);

console.log(result);