2016-06-10 2 views
0

페이지의 모든 데이터를 jsonp로 가져 오는 방법을 알아 내려고 노력했습니다.위키 백과 jsonp - 페이지 ID 추출하기

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

1 호 : 나는이 문제에 달려있다 나는 현재 정확한 링크를했지만 .. 이것은 내가 (예를 들어 스택 오버 플로우를 사용하여) JSONP를받을하기 위해 사용하고 URL은 나는 다음까지 얻을 수 있습니다 내 결과와

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을 수행 할 때, 나는 모든 정보를 얻을 수 있지만 페이지 번호를 전달하는 방법을 알아낼 수 없습니다 res.data.query.pages하지만 ofcourse 일단 내가 pageid 번호 (무작위입니다)에 도착하면 나는 어떻게 알아낼 수 없다. 정보를 얻으십시오.

2 호 : 나는 분명히 jsonp에서 extract을 얻고 싶지만 위의 내용을 보았을 지 모르지만이 모든 태그를 가지고 있으며 좋은 결과를 얻지 못할 것입니다. 표시 방법이 있습니까 이 HTML로? 다음과 같은 코드를 사용할 수있는 페이지 ID 추출하기 위해

답변

1

: 여기

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/ 추출물 속성 값을 취하고 ID 추출물 사업부에 추가 :

JS :

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

HTML :

<div id="extract"></div> 
같은 바이올린에서 당신은 여기에 업데이트가

추출물의 가치는 당연히 다른 방식으로 조작 할 수 있습니다.

+0

하나의 작은 질문, 하단에는 몇 가지 외부 링크가 있습니다. 그 링크를 json aswel로 연결하는 방법이 있습니까? 내 json 링크를 업데이트하고 매개 변수를 추가해야한다고 생각하지만 추가 할 항목이 확실하지 않습니다. – Borni

+0

json에서 다른 속성을 반환하도록 URL 구조를 변경하는 방법을 모르겠지만 해당 설명서에서 가능한 경우 찾을 수 있습니다. 또 다른 해결책은 추출 속성에서 url을 추출하여 배열에 추가하는 것입니다. 이 답변을 확인하여 regex를 사용하여 어떻게 수행되는지 확인할 수 있습니다. http://stackoverflow.com/questions/1986121/match-all-urls-in-string-and-return-in-array-in-javascript –