如何使用jQuery循环通过JSON文件 [英] How to loop through JSON file using jQuery

查看:108
本文介绍了如何使用jQuery循环通过JSON文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试通过以下JSON文件循环:

  {
statements:[{
subject:A
},{
predicate:B
},{
object:C
},{
subject:D
},{
predicate:E
},{
object:F
}]
}

正如你所看到的,两个谓词和两个对象。我想得到,例如,值谓词:E。我如何做这使用jQuery或D3的Javascript库。我的代码下面检索第一个主题subject:A

  $。each(data.statements [0],function(i,v){
console.log v.uriString);
});

或在D3中(我不知道如何在D3中执行此操作):

  d3.json(folder / sample.json,function(error,graph)
{//代码用于获取数据从JSON文件}

任何人都可以帮助我循环遍历上面的JSON文件,并检索一些特定数据

解决方案

请尝试以下操作:

  $(data.statements).each(function(i){
var d = data.statements [i];
$。每个(d,function(k,v){//获得对象
$(body)的键和值。append(< p>+ k +:+ v +& p>);
});
})



在这里转义


I am trying loop through the following JSON file below:

 {
     "statements": [{
         "subject": "A"
     }, {
         "predicate": "B"
     }, {
         "object": "C"
     }, {
         "subject": "D"
     }, {
         "predicate": "E"
     }, {
         "object": "F"
     }]
 }

As you can see, there are two subjects, two predicates and two objects. I would like to get, for instance, the value "predicate":"E". How can I do this using jQuery or D3 Javascript library. My code below retrieves the first subject "subject":"A".

$.each(data.statements[0], function(i, v){
console.log(v.uriString);
});

or in D3 (I do not know how to do this in D3):

d3.json("folder/sample.json", function(error, graph) 
{  // Code is here for getting data from JSON file }

Could anyone please help me loop through the JSON file above and retrieve some particular data either with jQuery or D3 Javascript. Thank you for your help in advance.

解决方案

Try this:

$(data.statements).each(function (i) {
    var d = data.statements[i];
    $.each(d, function (k, v) { //get key and value of object
        $("body").append("<p>"+k + ":" + v+"</p>");
    });
})

Fiddle here.

这篇关于如何使用jQuery循环通过JSON文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆