在JSON中搜索对象

时间:2011-05-24 12:23:04

标签: javascript json

{"widget": {
    "debug": "on",
    "window": {
        "title": "Sample Konfabulator Widget",
        "name": "main_window",
        "width": 500,
        "height": 500
    },
    "image": { 
        "src": "Images/Sun.png",
        "name": "sun1",
        "hOffset": 250,
        "vOffset": 250,
        "alignment": "center"
    },
    "text": {
        "data": "Click Here",
        "size": 36,
        "style": "bold",
        "name": "text1",
        "hOffset": 250,
        "vOffset": 100,
        "alignment": "center",
        "onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
    }
}}    

这是我的JSON字符串。现在我想在这个JSON中搜索名称,然后显示结果......

5 个答案:

答案 0 :(得分:7)

通过键迭代: (增强Amit Gupta的答案)

var result = [];
getNames(data, "name");
document.write("result: " + result.join(", "));

function getNames(obj, name) {
    for (var key in obj) {
        if (obj.hasOwnProperty(key)) {
            if ("object" == typeof(obj[key])) {
                getNames(obj[key], name);
            } else if (key == name) {
                result.push(obj[key]);
            }
        }
    }
}

工作演示@ http://jsfiddle.net/roberkules/JFEMH/

const data = {
  "widget": {
    "debug": "on",
    "window": {
      "title": "Sample Konfabulator Widget",
      "name": "main_window",
      "width": 500,
      "height": 500
    },
    "image": {
      "src": "Images/Sun.png",
      "name": "sun1",
      "hOffset": 250,
      "vOffset": 250,
      "alignment": "center"
    },
    "text": {
      "data": "Click Here",
      "size": 36,
      "style": "bold",
      "name": "text1",
      "hOffset": 250,
      "vOffset": 100,
      "alignment": "center",
      "onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
    }
  }
}

let result = [];
getNames(data, "title");
document.write("result: " + result.join(", "));

function getNames(obj, name) {
  for (var key in obj) {
    if (obj.hasOwnProperty(key)) {
      if ("object" == typeof(obj[key])) {
        getNames(obj[key], name);
      } else if (key == name) {
        result.push(obj[key]);
      }
    }
  }
}

答案 1 :(得分:3)

您可以使用Jquery来解析JSON

 $.ajax({
    type: "POST",
    url: "../JSON Source",
    success: function(msg) {
    var obj=jQuery.parseJSON(msg);
    if(obj.debug== "on"){
        //do anything

。 。 。 。

答案 2 :(得分:2)

您可以递归迭代到给定对象内的所有对象。

    s = "";

    function recursiveSearch(obj, name){
       if(typeof(obj)==="object" {
         for(var key in obj) {
            if (obj.hasOwnProperty(key)) {
              s += ":" + recursiveSearch(obj[key], name);
            }  
       } else if( typeof(obj["name"] != 'undefined') {
         s += ":" + obj["name"];

    }

输出将是带有键“name”的冒号分隔值

答案 3 :(得分:1)

我建议使用此JSON扩展程序injson。它允许您使用JQuery在JSON对象中搜索密钥。

答案 4 :(得分:1)

您可以使用DefiantJS(http://defiantjs.com)而不是编写自定义搜索功能,它可以使用XPath表达式对JSON结构进行查询。例如:

    var data = {
     "widget": {
        "debug": "on",
        "window": {
           "title": "Sample Konfabulator Widget",
           "name": "main_window",
           "width": 500,
           "height": 500
        },
        "image": {
           "src": "Images/Sun.png",
           "name": "sun1",
           "hOffset": 250,
           "vOffset": 250,
           "alignment": "center"
        },
        "text": {
           "data": "Click Here",
           "size": 36,
           "style": "bold",
           "name": "text1",
           "hOffset": 250,
           "vOffset": 100,
           "alignment": "center",
           "onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
        }
     }
  },
res = JSON.search( data, '//image[name]' );

console.log( res[0].name );

这是一个工作小提琴:
http://jsfiddle.net/hbi99/CRTz9/

DefiantJS使用方法“search”扩展全局对象JSON,并返回一个匹配的数组(如果没有找到匹配,则为空数组)。