确定JSON对象是否包含jQuery中的特定字段名

时间:2015-05-20 04:33:35

标签: javascript jquery json

是否可以确定JSON对象是否包含指定的“fieldname” 例如: 如果我有一个JSON对象,例如:

{"event":
    [
        {"query":
            {"id":
                [
                    {"timestamp_usec":"1316596939223064"}
                ],
            "query_text":"sometext"
            }
        },
        {"query":
            {"id":
                [
                    {"timestamp_usec":"1316318681908642","type":"sometype","abc":"someabc"},
                    {"timestamp_usec":"1316318679366796"}
                ],
            "query_text":"someothertext"
            }   
        }, 
        //...More...//
    ]   
}

我可以在if statement中使用哪些条件来确定特定“id对象”是否包含“abc”

更简单:

FOR ALL jsonpath id's
    IF jsonpath.id **CONTAINS** "abc" 
       THEN do something
    ELSE do something different
END LOOP

我正在寻找实现这个的jQuery函数(如果有的话!)。

我的代码:

$.getJSON("test.json", function(data) {
    var search = data.event
    $.each(data.event, function(i, item) { 
        var searchMeta = data.event[i].query.id;
        $.each(searchMeta[i], function(i, deeper){
            if (searchMeta[i].  == "abc"){
               //do something 
            } else { 
               //do something different
            }
        })
    })
});

我知道在上面的例子中,我基本上可以通过循环id对象的数量来实现我想要的,例如,如果num<但我不确定我的数据在多个.json文件中的统一程度。

2 个答案:

答案 0 :(得分:0)

JSON.stringify(json)这会给你json作为字符串然后你可以做REGEX来检查json中是否有文件。

答案 1 :(得分:0)

尝试定义一个函数来调用Object对象

中的每个data
   // property `name` to check
    var name = "abc";

    var check = function check(obj, name) {
        // if `obj` is `Object` ,
        // and `obj` has property `name` ,
        // do stuff
        if (/Object/.test(obj.constructor.toString()) 
           && obj.hasOwnProperty(name)) {
          console.log(name + " found")
        } 
        // else , do other stuff
        else {
          console.log(name + " not found")
        }
    };

    $.each(data.event, function(key, value) {
      check(value, name);
      $.each(value.query, function(k, v) {
        check(v, name);
          // if `v` is an `Object` , call check for 
          // call `check` on each `v` `Object` 
          if (typeof v === "object") {
            $.each(v, function(_k, _v) {
              check(_v, name);
            })
          };
      });
    });

var data = {
  "event": [{
      "query": {
        "id": [{
          "timestamp_usec": "1316596939223064"
        }],
        "query_text": "sometext"
      }
    }, {
      "query": {
        "id": [{
          "timestamp_usec": "1316318681908642",
          "type": "sometype",
          "abc": "someabc"
        }, {
          "timestamp_usec": "1316318679366796"
        }],
        "query_text": "someothertext"
      }
    }
    //...More...//
  ]
};

var data = {
  "event": [{
      "query": {
        "id": [{
          "timestamp_usec": "1316596939223064"
        }],
        "query_text": "sometext"
      }
    }, {
      "query": {
        "id": [{
          "timestamp_usec": "1316318681908642",
          "type": "sometype",
          "abc": "someabc"
        }, {
          "timestamp_usec": "1316318679366796"
        }],
        "query_text": "someothertext"
      }
    }
    //...More...//
  ]
};


// property `name` to check
var name = "abc";

var check = function check(obj, name) {
  // if `obj` is `Object` ,
  // and `obj` has property `name` ,
  // do stuff
  if (/Object/.test(obj.constructor.toString()) 
      && obj.hasOwnProperty(name)) {
    var res = {};
    res[name] = obj[name];
    $("body").append(JSON.stringify(res) + " found")
  }
  // else , do other stuff
  else {
    console.log(name + " not found")
  }
};

$.each(data.event, function(key, value) {
  check(value, name);
  $.each(value.query, function(k, v) {
    check(v, name);
    // if `v` is an `Object` , call check for 
    // call `check` on each `v` `Object` 
    if (typeof v === "object") {
      $.each(v, function(_k, _v) {
        check(_v, name);
      })
    };
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

jsfiddle http://jsfiddle.net/yenp6t8v/