You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用JavaScript或jQuery提取格式各异的嵌套JSON中的值?

提取嵌套JSON中格式差异的值(JavaScript/jQuery实现)

嘿,针对你这个嵌套JSON里提取差异格式值的需求,我来分享两种实用的实现方式——原生JavaScript和jQuery,结合你给的cart数据示例来讲解:

你的数据里核心的差异点在于contents数组中的对象有动态的产品键(比如"Apple iPhone"、"redmi"、"samsung"),而其他字段是固定的,所以我们的重点是精准定位这些动态键并提取对应的值。

一、原生JavaScript实现

1. 提取所有动态产品键的值

通过Object.keys()获取对象的所有键,再过滤掉已知的固定字段,就能得到动态的产品键,进而提取对应值:

var cart = [ { "Items": "", "category": "", "contents": [ { "Apple iPhone": "222", "French": "Bounjour", "id": 1234, "icon": "/images/bg.jpg", "callback": "use()", "pricetag":"false" } ] }, { "Items": "No 2", "category": "2nd", "contents": [ { "redmi": "333", "French": "some text", "id": 345787, "icon": "/images/bg.jpg", "callback": "use()", "pricetag":"true" }, { "samsung": "333", "French": "some text", "id": 86787876, "icon": "/images/bg.jpg", "callback": "use()", "pricetag":"disabled" } ] } ];

// 遍历外层cart数组
cart.forEach(cartItem => {
  // 遍历当前项的contents子数组
  cartItem.contents.forEach(content => {
    // 定义固定字段列表,用于过滤
    const fixedKeys = ['French', 'id', 'icon', 'callback', 'pricetag'];
    // 找到第一个非固定字段的动态产品键
    const productKey = Object.keys(content).find(key => !fixedKeys.includes(key));
    
    if (productKey) {
      const productValue = content[productKey];
      console.log(`产品名称: ${productKey}, 对应值: ${productValue}`);
      // 同时可以提取固定字段,比如产品ID
      console.log(`产品ID: ${content.id}`);
    }
  });
});

2. 提取特定条件的值(比如pricetag为true的产品)

如果需要筛选符合特定条件的内容,只需在遍历中加入判断逻辑即可:

cart.forEach(cartItem => {
  cartItem.contents.forEach(content => {
    // 筛选pricetag为"true"的项
    if (content.pricetag === 'true') {
      const fixedKeys = ['French', 'id', 'icon', 'callback', 'pricetag'];
      const productKey = Object.keys(content).find(key => !fixedKeys.includes(key));
      
      if (productKey) {
        console.log(`符合条件的产品: ${productKey}, 值: ${content[productKey]}`);
      }
    }
  });
});

二、jQuery实现

jQuery的遍历逻辑和原生类似,主要通过$.each()方法来处理数组:

1. 遍历提取所有动态产品值

$.each(cart, function(index, cartItem) {
  $.each(cartItem.contents, function(contentIndex, content) {
    const fixedKeys = ['French', 'id', 'icon', 'callback', 'pricetag'];
    const productKey = Object.keys(content).find(key => !fixedKeys.includes(key));
    
    if (productKey) {
      console.log(`产品名称: ${productKey}, 对应值: ${content[productKey]}`);
      console.log(`产品ID: ${content.id}`);
    }
  });
});

2. 筛选特定条件的内容

$.each(cart, function(index, cartItem) {
  $.each(cartItem.contents, function(contentIndex, content) {
    if (content.pricetag === 'true') {
      const fixedKeys = ['French', 'id', 'icon', 'callback', 'pricetag'];
      const productKey = Object.keys(content).find(key => !fixedKeys.includes(key));
      
      if (productKey) {
        console.log(`符合条件的产品: ${productKey}, 值: ${content[productKey]}`);
      }
    }
  });
});

关键说明

  • 这种过滤固定键的方法比直接取第一个键更可靠,即使动态产品键的位置不固定也能正常工作。
  • 如果存在多个动态产品键,只需把find换成filter,就能得到所有非固定键的数组,再逐个处理即可。

内容的提问来源于stack exchange,提问作者sonu kalkar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 03:50:33