【问题标题】:How to Get JSON data keys and inner keys to from new object in Javascript如何从 Javascript 中的新对象获取 JSON 数据键和内部键
【发布时间】:2017-10-04 05:40:06
【问题描述】:

我有一个 JSON,我需要从中提取 JSON 中的所有键。到目前为止,我提取了所有键,但我需要从只有键值的多维数组中提取。

{
  "coord": {
    "lon": -0.13,
    "lat": 51.51
  },
  "weather": [
    {
      "id": 300,
      "main": {
        "submain": "Drizzle"
      },
      "description": "light intensity drizzle",
      "icon": "09d"
    }
  ]
}

我需要构造类似这样的多维数组...

["coord",["lon","lat"],"weather",[["id","main","description",["small_descript","large_description"],"icon"]....] 
我使用的代码是...

var keyValue1= [];
function keyList(obj) 
{
	Object.keys(obj).forEach(function(key) {
    	keyValue1.push(key);
	    if(typeof(obj[key]) == 'object')
	    {
	    	keyList(obj[key]);
	    } 
	});
	
}


var obj = {"coord":{"lon":-0.13,"lat":51.51},"weather":[{"id":300,"main":"Drizzle","description":{"small_descript":"light intensity drizzle","large_descript":"light intensity drizzle"},"icon":"09d"}],"base":"stations","main":{"temp":280.32,"pressure":1012,"humidity":81,"temp_min":279.15,"temp_max":281.15},"visibility":10000,"wind":{"speed":4.1,"deg":80},"clouds":{"all":90},"dt":1485789600,"sys":{"type":1,"id":5091,"message":0.0103,"country":"GB","sunrise":1485762037,"sunset":1485794875},"id":2643743,"name":"London","cod":200};
keyList(obj);
console.log(keyValue1);

【问题讨论】:

    标签: javascript jquery json


    【解决方案1】:

    你可以试试这个sn-p(这很不言自明)

    let getKeyList = obj => Object.keys(obj).reduce((s, key) => {
      if (Array.isArray(obj[key])) {
        // if it's an array then reduce all it's values (keys) into an array of arrays...
        s.push(obj[key].reduce((acc, sub) => {
          acc.push(getKeyList(sub));
          return acc;
        }, []))
      } else {
        // if it's an object then recurse...
        if (typeof(obj[key]) === "object")
          s.push(getKeyList(obj[key]))
      }
      // always push the key
      s.push(key)
      return s;
    }, [ /* store */ ]);
    
    
    // the JSON data
    var x = {"coord":{"lon":-0.13,"lat":51.51},"weather":[{"id":300,"main":"Drizzle","description":{"small_descript":"light intensity drizzle","large_descript":"light intensity drizzle"},"icon":"09d"}],"base":"stations","main":{"temp":280.32,"pressure":1012,"humidity":81,"temp_min":279.15,"temp_max":281.15},"visibility":10000,"wind":{"speed":4.1,"deg":80},"clouds":{"all":90},"dt":1485789600,"sys":{"type":1,"id":5091,"message":0.0103,"country":"GB","sunrise":1485762037,"sunset":1485794875},"id":2643743,"name":"London","cod":200};
    
    // then call it as
    console.log(getKeyList(x));

    【讨论】:

      【解决方案2】:

      您可以将 reduce 与一些经常读取的对象字段一起使用::

      function keyList(obj) {
      	return Object.keys(obj).reduce(function(arr, key) {
          arr.push(key);
          if(typeof(obj[key]) === 'object') {
            arr.push(keyList(obj[key]));
          }
          return arr;
      	}, []);
      	
      }
      
      
      var obj = {
        "coord": {
          "lon": -0.13,
          "lat": 51.51
        },
        "weather": [
          {
            "id": 300,
            "main": "Drizzle",
            "description": {
              "small_descript": "light intensity drizzle",
              "large_descript": "light intensity drizzle"
            },
            "icon": "09d"
          }
        ],
        "base": "stations",
        "main": {
          "temp": 280.32,
          "pressure": 1012,
          "humidity": 81,
          "temp": {
            "min": 279.15,
            "max": 281.15
          }
        },
        "visibility": 10000,
        "wind": {
          "speed": 4.1,
          "deg": 80
        },
        "clouds": {
          "all": 90
        },
        "dt": 1485789600,
        "sys": {
          "type": 1,
          "id": 5091,
          "message": 0.0103,
          "country": "GB",
          "sunrise": 1485762037,
          "sunset": 1485794875
        },
        "id": 2643743,
        "name": "London",
        "cod": 200
      }
      var keyValue1 = keyList(obj);
      console.log(keyValue1);

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-04-03
        • 2019-07-07
        • 2020-08-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多