维基百科jsonp - 提取页面ID

时间:2016-06-10 19:03:26

标签: angularjs api jsonp wikipedia

我一直试图弄清楚如何将页面中的所有数据转换为jsonp。 我目前有正确的链接,但我遇到了2个问题..这是我用来接收jsonp的URL(以堆栈溢出为例):

https://en.wikipedia.org/w/api.php?action=query&prop=extracts&titles=stack%20overflow&format=json&callback=JSONP_CALLBACK

第一期: 当我做res.data时,我得到了所有的信息,但我似乎无法弄清楚如何通过页码:

JSONP_CALLBACK (
{
"batchcomplete": "",
"query": {
"normalized": [
{
"from": "stack overflow",
"to": "Stack overflow"
}
],
"pages": {
"1436888": { // This is where I get stuck..
"pageid": 1436888,
"ns": 0,
"title": "Stack overflow",
"extract": "<p>In software, a <b>stack overflow</b> occurs if the call stack pointer exceeds the stack bound. The call stack may consist of a limited amount of address space, often determined at the start of the program. The size of the call stack depends on many factors, including the programming language, machine architecture, multi-threading, and amount of available memory. When a program attempts to use more space than is available on the call stack (that is, when it attempts to access memory beyond the call stack's bounds, which is essentially a buffer overflow), the stack is said to <i>overflow</i>, typically resulting in a program crash.</p>\n<p></p>\n<h2><span id=\"Infinite_recursion\">Infinite recursion</span></h2>\n\n<p>The most common cause of stack overflow is excessively deep or infinite recursion, in which a function calls itself so many times that the space needed to store the variables and information associated with each call is more than can fit on the stack.</p>\n<p>An example of infinite recursion in C.</p>\n\n<p>The function <i>foo</i>, when it is invoked, continues to invoke itself, allocating additional space on the stack each time, until the stack overflows resulting in a segmentation fault. However, some compilers implement tail-call optimization, allowing infinite recursion of a specific sort—tail recursion—to occur without stack overflow. This works because tail-recursion calls do not take up additional stack space.</p>\n<p>C compiler options will effectively enable tail-call optimization; compiling the above simple program using gcc with <code>-O1</code> will result in a segmentation fault, but not when using <code>-O2</code> or <code>-O3</code>, since these optimization levels imply the <code>-foptimize-sibling-calls</code> compiler option. Other languages, such as Scheme, require all implementations to include tail-recursion as part of the language standard.</p>\n<h2><span id=\"Very_deep_recursion\">Very deep recursion</span></h2>\n<p>A recursive function that terminates in theory but causes a call stack buffer overflow in practice can be fixed by transforming the recursion into a loop and storing the function arguments in a stack. This is always possible, because the class of primitive recursive functions is equivalent to the class of LOOP computable functions. Consider this example in C++-like pseudocode:</p>\n<p>A primitive recursive function like the one on the left side can always be transformed into a loop like on the right side.</p>\n<h2><span id=\"Very_large_stack_variables\">Very large stack variables</span></h2>\n<p>The other major cause of a stack overflow results from an attempt to allocate more memory on the stack than will fit, for example by creating local array variables that are too large. For this reason some authors recommend that arrays larger than a few kilobytes should be allocated dynamically instead of as a local variable.</p>\n<p>An example of a very large stack variable in C:</p>\n\n<p>The declared array consumes 8 mebibytes of data (assuming each double is 8 bytes); if this is more memory than is available on the stack (as set by thread creation parameters or operating system limits), a stack overflow will occur.</p>\n<p>Stack overflows are made worse by anything that reduces the effective stack size of a given program. For example, the same program being run without multiple threads might work fine, but as soon as multi-threading is enabled the program will crash. This is because most programs with threads have less stack space per thread than a program with no threading support. Because kernels are generally multi-threaded, people new to kernel development are usually discouraged from using recursive algorithms or large stack buffers.</p>\n<h2><span id=\"See_also\">See also</span></h2>\n\n<ul><li>Buffer overflow</li>\n<li>Call stack</li>\n<li>Heap overflow</li>\n<li>Stack buffer overflow</li>\n<li>Double fault</li>\n</ul><h2><span id=\"References\">References</span></h2>\n\n<p>Kernel Programming Guide https://developer.apple.com/library/mac/documentation/Darwin/Conceptual/KernelProgramming/KernelProgramming.pdf</p>\n<h2><span id=\"External_links\">External links</span></h2>\n<ul><li>The reasons why 64-bit programs require more stack memory</li>\n</ul>"
    }
   }
  }
 }
)

根据我的结果,我可以达到以下目的:res.data.query.pages但是当我到达pageid号码(这是随机的)时,我似乎无法弄清楚如何获取信息。

问题2: 我希望显然从jsonp获得extract,但正如您可能已经看到的那样,它具有所有这些标签,并且不会提供良好的输出..有没有办法将其显示为html?

1 个答案:

答案 0 :(得分:1)

要提取页面ID,您可以使用以下代码:

var theObj = res.data.query.pages;
var thePageId = theObj[Object.keys(theObj)[0]].pageid;
// Object.keys(theObj)[0] will extract the value of the first property which is variable in your case and you don't know it. So we have the object which we know it has the property "pageid".

这里有一个提示页面ID的小提琴:https://jsfiddle.net/fg6mdrxj/

关于第二个问题,一旦获得属性的值,就可以轻松地操作它。 在同一个小提琴中你有一个更新:https://jsfiddle.net/fg6mdrxj/1/取得extract属性的值并将其附加到具有id提取的div:

JS:

var toHtml = theObj[Object.keys(theObj)[0]].extract;
document.getElementById("extract").innerHTML = toHtml;

HTML:

<div id="extract"></div>

当然,一旦你得到它,就可以以不同的方式操纵提取物的价值。