【问题标题】:Extract JSON object values/keys without using hardcoded keys using Javascript使用 Javascript 在不使用硬编码键的情况下提取 JSON 对象值/键
【发布时间】:2018-01-18 12:19:47
【问题描述】:
 cart = {
 "Items": 3,
 "Item": {
      "Apple iPhone 5S": {
              "productId": 688,
              "url": "http://website.com/phone_iphone5s.html",
              "price": 299.99
        },
      "Solio Mono Solar Charger": {
              "productId": 655,
              "url": "http://website.com/solio_charger.html",
              "price": 29.95
        },
       "24 Month Warranty Package": {
              "productId": 681,
              "url": "http://website.com/24_month_warranty.html",
              "price": 129.95
        }
  },
"Total": 459.89
 }

我想编写一个 JavaScript 函数,在浏览器开发工具控制台中输出商品总数、每个商品的价格以及购物车的总价值。例如:需要选择Apple Iphone 5S 及其价格、Solio Mono Solar Charger 及其价格等。不使用硬编码键(例如“Apple iPhone 5S”、“Solio Mono Solar Charger”)。有什么办法吗??

这种格式可以获取吗??

   Items: 3 
    - Apple iPhone 5S ($299.99) 
    - Solio Mono Solar Charger ($29.95) 
    - 24 Month Warranty Package ($129.95)
   Total: 459.89 

【问题讨论】:

标签: javascript jquery json object jsonparser


【解决方案1】:

你在找这样的东西吗

var str = `Items: ${cart.Items} `;
Object.keys(cart.Item).forEach((key) => {
  str += `- ${key} (${cart.Item[key].price}) `
})

str +=  `Total: ${cart.Total}`
console.log(str)

var cart = {
  "Items": 3,
  "Item": {
    "Apple iPhone 5S": {
      "productId": 688,
      "url": "http://website.com/phone_iphone5s.html",
      "price": 299.99
    },
    "Solio Mono Solar Charger": {
      "productId": 655,
      "url": "http://website.com/solio_charger.html",
      "price": 29.95
    },
    "24 Month Warranty Package": {
      "productId": 681,
      "url": "http://website.com/24_month_warranty.html",
      "price": 129.95
    }
  },
  "Total": 459.89
}


var str = `Items: ${cart.Items} `;
Object.keys(cart.Item).forEach((key) => {
  str += `- ${key} (${cart.Item[key].price}) `
})

str +=  `Total: ${cart.Total}`
console.log(str)

【讨论】:

  • 这种格式可以获取吗??项目:3 - Apple iPhone 5S ($299.99) - Solio Mono 太阳能充电器 ($29.95) - 24 个月保修包 ($129.95) 总计:459.89
  • 但是我们可以使用 Javascript 代替 Jquery 吗?
  • 以上代码在 ES6 标准中的纯 JS 上,而不是 jquery
【解决方案2】:

您可以像这样遍历它们并获取想要的数据。

您可以根据需要对其进行编辑。

cart = {
 "Items": 3,
 "Item": {
      "Apple iPhone 5S": {
              "productId": 688,
              "url": "http://website.com/phone_iphone5s.html",
              "price": 299.99
        },
      "Solio Mono Solar Charger": {
              "productId": 655,
              "url": "http://website.com/solio_charger.html",
              "price": 29.95
        },
       "24 Month Warranty Package": {
              "productId": 681,
              "url": "http://website.com/24_month_warranty.html",
              "price": 129.95
        }
  },
"Total": 459.89
 }

for (var key in cart.Item) {
    if (cart.Item.hasOwnProperty(key)) {
        console.log(key + " " + cart.Item[key]);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-12
    • 1970-01-01
    • 2021-03-15
    • 1970-01-01
    • 2021-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多