代码之家  ›  专栏  ›  技术社区  ›  stoesselleon

嵌套JSON访问值

  •  2
  • stoesselleon  · 技术社区  · 6 年前

    {
        "batchcomplete":"",
        "query":{
            "pages":{
                "104352":{
                    "pageid":104352,
                    "ns":0,
                    "title":"student"
                }
            }
        }
    }

    应该

    2 回复  |  直到 6 年前
        1
  •  2
  •   Jordan Soltman    6 年前

    const obj = {
      "batchcomplete": "",
      "query": {
        "pages": {
          "104352": {
            "pageid": 104352,
            "ns": 0,
            "title": "student"
          }
        }
      }
    }
    
    // Get the keys for pages
    const keys = Object.keys(obj.query.pages);
    
    // Print out the first key
    console.log(keys[0]);
        2
  •  0
  •   Slai Pslice    6 年前

    var n, j = '{"batchcomplete":"","query":{"pages":{"104352":{"pageid":104352,"ns":0,"title":"student"}}}}';
    
    var result = JSON.parse(j, (key, value) => (n = n || +key, value));
    
    console.log( n ); console.log( result );