2017-11-17 1 views
0

내 페이지에 두 개의 JSON이 있습니다. 두 번째 페이지에는 특정 객체 'articleSection'이 들어 있으며 첫 번째 페이지에는 없습니다. JSON이 발견 될 때까지 모든 스크립트를 반복하고, 'articleSection이 있는지'를 확인하기 위해 각 JSON을 반복합니다. 그것이 존재한다면, 나는 'articleSection'의 가치를 돌려주고 싶다.페이지의 모든 JSON을 반복하고 특정 ID를 찾으십시오.

나는 지금까지이 모든 것을 얻었지만 반환 값은 항상 정의되지 않았습니다.

:

function getJSON() { 
    var JSONS = document.getElementsByTagName("Script") 

    for (i=0; i<JSONS.length;i++){ 
    if (JSONS[i].getAttribute("type") == "application/ld+json"){ 
     var jsonParsed = JSON.parse(JSONS[i].innerText); 
      if (jsonParsed.articleSection != null && jsonParsed.articleSection != ""){ 
      return jsonParsed.articleSection; 
      } 
     } 
} 
} 

나의 생각은 내가 제대로 만에 refrence에 대한 초기 if JSONS[i].getAttribute("type") == "application/ld+json

JSON에서 발견되는 첫 번째 통해 오히려 JSON 스크립트의 각 통해 반복,하지만 오전입니다 JSON ONE :

<script type="application/ld+json"> 
     { 
     "@context" : "http://schema.org", 
     "@type" : "Organization", 
     } 
    </script> 

JSON 2 :

<script type="application/ld+json"> 
     { 
      "@context": "http://schema.org", 
      "@type": "Article", 
      "keywords": "", 
      "author": { 
      "@type": "Person", 
      "name": "Staff" 
      }, 
      "articleSection": "Christmas, Festivals, Events, Life, Arts" 
} 
+1

'내 page' 두 JSON이가 내 page' 3 HTML이'말처럼 - 그것은 이해가되지 않습니다. – ProEvilz

+1

당신의 아들은 어디 있습니까? – Joschi

+0

방금 ​​추가 된 – SFro

답변

0

당신은 null이면서 비어 있지만 정의되지 않은 것은 테스트 중입니다. 똑같은 것이 아닙니다.

아마도 이런 일이 일어날 수 있습니다. 파싱 ​​된 첫 번째 객체는 articleSection 속성이없는 객체입니다. 이 경우 jsonParsed.articleSection은 정의되지 않았으므로 null 및 빈 문자열에 대해 테스트하면 루프 내에서 계속 이동합니다. 그리고 정의되지 않은 반환합니다.

빈 문자열에 대해서는 테스트하지 않는 것이 좋습니다. 당신이 if (jsonParsed.articleSection){을 사용할 수 있습니다 jsonParsedarticleSection을 확인하려면 널 (null)과

+0

은 null과 정의되지 않은 값을 테스트했지만 아직 정의되지 않은 값을 반환하도록 업데이트되었습니다. if (jsonParsed.articleSection! == undefined && jsonParsed.articleSection! == null) – SFro

0

정의에 대한 그냥 테스트는,이 null, undefined 빈 문자열을 확인합니다. 코드가 작동하지 않는 이유는 JSON은 JSON

<script type="application/ld+json"> 
    { 
    "@context" : "http://schema.org", 
    "@type" : "Organization", 
    } 
</script> 

당신이 ,'Organization' 후, 그것을 제거하고 그것을 작동합니다 같이 잘못했다입니다.

라이브 데모

function getJSON() { 
 
    var JSONS = document.querySelectorAll("script[type='application/ld+json']"); 
 
    for (var i=0; i<JSONS.length;i++){ 
 
     var jsonParsed = JSON.parse(JSONS[i].innerHTML); 
 
     if (jsonParsed.articleSection){ 
 
     return jsonParsed.articleSection; 
 
     } 
 
    } 
 
} 
 
console.log(getJSON());
<script type="application/ld+json"> 
 
    { 
 
    "@context": "http://schema.org", 
 
    "@type": "Organization" 
 
    } 
 
</script> 
 

 
<script type="application/ld+json"> 
 
    { 
 
    "@context": "http://schema.org", 
 
    "@type": "Article", 
 
    "keywords": "", 
 
    "author": { 
 
     "@type": "Person", 
 
     "name": "Staff" 
 
    }, 
 
    "articleSection": "Christmas, Festivals, Events, Life, Arts" 
 
    } 
 
</script>

+0

죄송합니다. 추가 세부 사항을 제공해야합니다. 이것은 실제로 작동하지만이 사용자 정의 자바 스크립트를 사용하여 변수로 설정 한 Google 태그 관리자에서는 작동하지 않는 것으로 보입니다. 여전히 정의되지 않은 것으로 반환됩니다 (JSON이 추가 쉼표도 제거되도록 정리되었습니다) – SFro

+0

'var JSONS = document.querySelectorAll ("script [type ='application/ld + json ']");'주어진 속성을 가진 스크립트를 얻으려면 for 루프 안에서 if 조건을 제거해야합니다. Google 태그 관리자에서'console.log()'를 실행하여 결과를 가져 오는 지 여부를 확인하십시오. –

관련 문제