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

如果json键总是以不同/随机的名称返回,如何绑定到json键?

  •  3
  • user2094257  · 技术社区  · 7 年前

    我正忙于一个有棱角的4。我试图在x应用程序中显示一篇随机的维基百科文章。我在chrome开发工具的query/pages-alwasys下看到的JSON有一个不同的带数字的pageID。他们不是以数组的形式返回页面,而是以JSON对象的形式返回,JSON对象总是有不同的名称,所以我不确定如何将其转换为typescript接口或绑定到它,因为名称总是不同的?你知道我该怎么办吗?

    我从维基百科api获得的JSON:

    {
      "batchcomplete": "",
      "continue": {
        "grncontinue": "0.241230031087|0.241230543855|19422120|0",
        "continue": "grncontinue||"
      },
      "warnings": {
        "extracts": {
          "*": "\"exlimit\" was too large for a whole article extracts request, lowered to 1."
        }
      },
      "query": {
        "pages": {
          "742585": {
            "pageid": 742585,
            "ns": 0,
            "title": "Speedway",
            "extract": "<p><b>Speedway</b> may refer to:</p>\n<h2><span id=\"In_racing\">In racing</span></h2>\n<ul><li>Oval track racing, motor racing on an oval track which turns in one direction</li>\n<li>Cycle speedway, a form of bicycle racing</li>\n<li>Motorcycle speedway, a form of motorcycle sport</li>\n<li>Dirt track racing, known as speedway in Australia and New Zealand</li>\n</ul><h2><span id=\"Other_uses\">Other uses</span></h2>\n<dl><dt>Placenames</dt>\n</dl><ul><li>Speedway, California, former town in Butte County</li>..."
          }
        }
      }
    }
    
    3 回复  |  直到 7 年前
        1
  •  4
  •   Sibiraj Marko AvlijaÅ¡    7 年前

    使用 Object.keys 以数组形式获取对象内的关键帧

    var data = {
      "batchcomplete": "",
      "continue": {
        "grncontinue": "0.241230031087|0.241230543855|19422120|0",
        "continue": "grncontinue||"
      },
      "warnings": {
        "extracts": {
          "*": "\"exlimit\" was too large for a whole article extracts request, lowered to 1."
        }
      },
      "query": {
        "pages": {
          "742585": {
            "pageid": 742585,
            "ns": 0,
            "title": "Speedway",
            "extract": "<p><b>Speedway</b> may refer to:</p>\n<h2><span id=\"In_racing\">In racing</span></h2>\n<ul><li>Oval track racing, motor racing on an oval track which turns in one direction</li>\n<li>Cycle speedway, a form of bicycle racing</li>\n<li>Motorcycle speedway, a form of motorcycle sport</li>\n<li>Dirt track racing, known as speedway in Australia and New Zealand</li>\n</ul><h2><span id=\"Other_uses\">Other uses</span></h2>\n<dl><dt>Placenames</dt>\n</dl><ul><li>Speedway, California, former town in Butte County</li>..."
          }
        }
      }
    };
    
    var MyArray = Object.keys(data.query.pages);
    
    console.log('MyArray: '+ JSON.stringify(MyArray));
    
    // Required Data
    
    console.log('Page: '+ data.query.pages[MyArray[0]].pageid)
        2
  •  1
  •   jitender    7 年前

    Object.keys 方法类似

    var obj={
      "batchcomplete": "",
      "continue": {
        "grncontinue": "0.241230031087|0.241230543855|19422120|0",
        "continue": "grncontinue||"
      },
      "warnings": {
        "extracts": {
          "*": "\"exlimit\" was too large for a whole article extracts request, lowered to 1."
        }
      },
      "query": {
        "pages": {
          "742585": {
            "pageid": 742585,
            "ns": 0,
            "title": "Speedway",
            "extract": "<p><b>Speedway</b> may refer to:</p>\n<h2><span id=\"In_racing\">In racing</span></h2>\n<ul><li>Oval track racing, motor racing on an oval track which turns in one direction</li>\n<li>Cycle speedway, a form of bicycle racing</li>\n<li>Motorcycle speedway, a form of motorcycle sport</li>\n<li>Dirt track racing, known as speedway in Australia and New Zealand</li>\n</ul><h2><span id=\"Other_uses\">Other uses</span></h2>\n<dl><dt>Placenames</dt>\n</dl><ul><li>Speedway, California, former town in Butte County</li>..."
          }
        }
      }
    }
    
    var pageNo=Object.keys(obj.query.pages)[0];
    
    console.log(pageNo)
        3
  •  0
  •   Tgr    7 年前

    使用 formatversion=2 :

    action=query&format=json&prop=extracts&exlimit=1&generator=random&formatversion=2

    {
        "batchcomplete": true,
        "continue": {
            "grncontinue": "0.451777061970|0.451777269201|12035865|0",
            "continue": "grncontinue||"
        },
        "query": {
            "pages": [
                {
                    "pageid": 18652441,
                    "ns": 0,
                    "title": "Kuczyny",
                    "extract": "<p><b>Kuczyny</b> <span>[kuˈt͡ʂɨnɨ]</span> is a village in the administrative district of Gmina Stawiski, within Kolno County, Podlaskie Voivodeship, in north-eastern Poland. It lies approximately 4 kilometres (2 mi) north of Stawiski, 15 km (9 mi) east of Kolno, and 75 km (47 mi) north-west of the regional capital Białystok.</p>\n<p>The village has a population of 31.</p>\n<h2><span id=\"References\">References</span></h2>\n\n<p><br></p>\n\n<p><span></span></p>"
                }
            ]
        }
    }