Javascript从json格式解析

时间:2015-03-11 03:58:37

标签: javascript json

我有解析javascript的问题。我想输出标题永久链接的值和标记为" image_01"的基本名称的值。因此它只会输出http://blog.url/news/images/201516.jpg

<div id="placeholder"></div>
<script>
var data={
   "items": [
      {
         "title": "HELLO THIS IS TITLE",
         "customFields": [
            {
               "basename": "image_01",
               "value": "<form mt:asset-id=\"352706\" class=\"mt-enclosure mt-enclosure-image\" style=\"display: inline;\"><a href=\"http://blog.url/news/images/201516.jpg\">201516.jpg<\/a><\/form>"
            },
            {  
               "basename":"image_02",
               "value":"<form mt:asset-id=\"352705\" class=\"mt-enclosure mt-enclosure-image\" style=\"display: inline;\"><a href=\"http://blog.url/news/images/201516.jpg\">201517.jpg<\/a><\/form>"
            },
            {  
               "basename":"image_03",
               "value":""
            },
         ],
         "permalink": "http://blog.com/index.php"
      }
   ]
}

document.getElementById("placeholder").innerHTML=data.items[0].title + " " + data.items[0].permalink+"--"+ data.items[0].customFields[0].basename[0].image_01;
    </script>

http://jsfiddle.net/v57s2csa/3/

2 个答案:

答案 0 :(得分:0)

  1. basename不是任何数组,因此您无法使用basename [0]进行访问。
  2. “image_01”不是任何财产。
  3. 所以用customFields [0] .value属性替换customFields [0] .basename [0] .image_01,该属性的图像路径为basename,标记为“image_01”。

    document.getElementById("placeholder").innerHTML=data.items[0].title + " " + data.items[0].permalink+"--"+ data.items[0].customFields[0].value;
    

答案 1 :(得分:0)

我认为@Kanti给出了正确答案,你可以更具体:

document.getElementById("placeholder").innerHTML=data.items[0].title + " " + data.items[0].permalink+"--"+ data.items[0].customFields[0].basename;