Json - 获得特定领域

时间:2017-04-14 12:02:51

标签: javascript jquery json

我需要从 JSON 获取字段来源,但我的解决方案无效!!

这是json:

"trailers":{
      "quicktime":[],
      "youtube":[{
                 "name":"BandeAnnonce",
                 "size":"HD",
                 "source":"RqEuaM9Fsrg",
                 "type":"Trailer"
                }]
 }

我尝试获取来源var link_trailer = data.trailers[0].youtube[0].source; 但是不适合我!!

3 个答案:

答案 0 :(得分:1)

预告片是对象,您不需要使用索引。

TEXT

答案 1 :(得分:0)

尝试以下代码

var text = '{' +
    '"trailers": {' +
    '"quicktime": [],' +
    '"youtube": [{' +
    '"name": "BandeAnnonce",' +
    '"size": "HD",' +
    '"source": "RqEuaM9Fsrg",' +
    '"type": "Trailer"' +
    '}]' +
    '}' +
    '}';

obj = JSON.parse(text);
var source = obj.trailers.youtube[0].source;
document.getElementById("demo").innerHTML ="Source field value is :"+source;

这是工作的jsfiddle:http://jsfiddle.net/NJMyD/5387/

答案 2 :(得分:-2)

首先将json解析为对象

{ "attendees": [ { "datum": "Tue, 11 Apr 2017 00:00:00 GMT", "name": " Muylaert-Geleir", "prename": "Alexander" }, { "datum": "Wed, 12 Apr 2017 00:00:00 GMT", "name": " Obolina", "prename": "Angelina" }, { "datum": "Thu, 13 Apr 2017 00:00:00 GMT", "name": " Obolina", "prename": "Angelina" }, ] }