开发者

Searching for an Object inside the JSON

{"widget": {
    "debug": "on",
    "window": {
        "title": "Sample Konfabulator Widget",
        "na开发者_运维技巧me": "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;"
    }
}}    

Here is my JSON String. Now i want to search for name in this JSON and then display the results...


Iterate through the keys: (enhancement to Amit Gupta's answer)

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]);
            }
        }
    }
}

Working demo @ 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]);
      }
    }
  }
}


You can use Jquery to parse JSON

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

. . . . .


You can iterate recursively to all the objects inside the given object.

    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"];

    }

Output will be colon separated values with key "name"


I would recommend using this JSON extension injson . It allows for you to use JQuery to search for a key in a JSON object.


Instead of writing custom search functions, you can use DefiantJS (http://defiantjs.com) which enables queries on JSON structures with XPath expressions. Example:

    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 );

Here is a working fiddle:
http://jsfiddle.net/hbi99/CRTz9/

DefiantJS extends the global object JSON with the method "search" and returns an array with the matches (empty array if no matches were found).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜