如何在JSON对象中搜索特定的字符串值?

时间:2016-12-13 16:15:40

标签: javascript html arrays json

我想搜索JSON对象以检查它是否包含存储在数组中的字符串值。并找出父元素。

var searchVal = ['name_1a','name_2y','name_3x'];

var json = {
"location": {
        "title1x": {
            "1": "name_1x",
            "2": "name_2x",
            "3": "name_3x",
        },
       "title2y": {
            "1": "name_1y",
            "2": "name_2y",
            "3": "name_3y",
        },
    }

    "object": {
        "title1a": {
            "1": "name_1z",
            "2": "name_2z",
            "3": "name_3z",
        },
       "title2b": {
            "1": "name_1a",
            "2": "name_2a",
            "3": "name_3a",
        },
    }
};

我想将结果传递给函数。并单独处理它们。

name_1a -> function(title2b, object)
name_2y -> function(title2y, object)
name_3x -> function(title1x, location) etc.

。 这是我到目前为止所尝试的。我似乎无法弄清楚如何通过整个 JSON对象

var searchVal = ['name_1a','name_2y','name_3x'];
  for (var i=0 ; i < searchVal.length ; i++){
    for (var k=0 ; k < ????.length ; k++)
    {
      if (json.???????? == searchVal[i]) {
        results.push(???????);
        console.log(results);

      }
    }
  }

3 个答案:

答案 0 :(得分:1)

使用下面的代码,您可以递归地找到您要查找的内容:

struct generic_t
{
    void* obj;
    void(*del)(void*);
};
template <typename T> // outer template parameter
generic_t copy_to_generic(const T& value)
{
    struct local_cast // local class
    {
        static void destroy(void* p) // void*-based interface
        {
            delete static_cast<T*>(p); // static type knowledge
        }
    };
    generic_t p;
    p.obj = new T(value); // information loss: copy T* to void*
    p.del = &local_cast::destroy;
    return p;
}

参考:

get data from dynamic key value in json

get value from json with dynamic key

https://trinitytuts.com/tips/get-dynamic-keys-from-json-data/

How do I enumerate the properties of a JavaScript object?

Check if a value is an object in JavaScript

答案 1 :(得分:1)

这样的事情:

var json = {
  "location": {
    "title1x": {
      "1": "name_1x",
      "2": "name_2x",
      "3": "name_3x",
    },
    "title2y": {
      "1": "name_1y",
      "2": "name_2y",
      "3": "name_3y",
    },
  },
  "object": {
    "title1a": {
      "1": "name_1z",
      "2": "name_2z",
      "3": "name_3z",
    },
    "title2b": {
      "1": "name_1a",
      "2": "name_2a",
      "3": "name_3a",
      "foo": [{
        "x": "aaa",
        "y": "bbb",
        "z": {
          "k": "name_3y"
        }
      }, {
        "x": "aaa",
        "y": "bbb",
        "z": {
          "k": "name_3y",
          "bar": [{
            "op": "test",
            "fooAgain": {
              "x": "name_3y"
            }
          }]
        }
      }]
    },
  }
};

function search(what, where) {

  var results = [];
  var parentStack = [];

  var searchR = function(what, where) {

    if (typeof where == "object") {
      parentStack.push(where);
      for (key in where) {
        searchR(what, where[key]);
      };
      parentStack.pop();
    } else {
      // here comes your search
      if (what === where) {
        results.push({
          parent: parentStack[parentStack.length - 1],
          value: where
        });
      }
    }

  }

  searchR(what, where);

  return results;

}

search("name_3y", json).forEach(function(value, key) {

  var out = "parent: \n";

  for (key in value.parent) {
    out += "    key: " + key + " - value: " + value.parent[key] + "\n";
  }

  out += "\nvalue: " + value.value;

  alert(out);

});

搜索功能将在json对象内搜索完全相等的值。例如,您可以使用它来搜索数组的每个元素,只需调整代码即可。堆栈是必要的,因为我们需要跟踪父母。我修改了你的json以插入更多关卡。结果的值是具有两个属性的对象。我认为通过这个你可以做你需要的。当然,您可以修改我的代码,以便在搜索中使用正则表达式的字符串。它会更强大。

答案 2 :(得分:0)

&#13;
&#13;
var searchVal = ['name_1a','name_2y','name_3x'];

var json = {
"location": {
        "title1x": {
            "1": "name_1x",
            "2": "name_2x",
            "3": "name_3x",
        },
       "title2y": {
            "1": "name_1y",
            "2": "name_2y",
            "3": "name_3y",
        }
    },

    "object": {
        "title1a": {
            "1": "name_1z",
            "2": "name_2z",
            "3": "name_3z",
        },
       "title2b": {
            "1": "name_1a",
            "2": "name_2a",
            "3": "name_3a",
        }
    }
};
var getTitle=function(json,val){
  for (var key in json) {
    var titles= json[key];
    for (var tit in titles) {
      var names=titles[tit];
      for (var name in names) {
        var string=names[name];
        if(string===val)
          return tit;
      }
    }
}
}

searchVal.forEach(function(valToSearch){
   console.log(getTitle(json,valToSearch));
});
&#13;
&#13;
&#13;