Javascript 如何在json中找到值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34450904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find value in json
提问by Jayesh Vekariya
How to fine state name using postcode in bellow json data;
如何在下面的 json 数据中使用邮政编码来细化状态名称;
var data = '{
"1": {
"state": "VIC",
"postcode": "2600,2603,2605,2606"
},
"2": {
"state": "NSW",
"postcode": "2259,2264"
}
}'
How to find state
by postcode
;
如何找到state
的postcode
;
if i search postcode 2600
if get result like VIC
如果我搜索邮政编码2600
如果得到结果VIC
采纳答案by void
Remove ''
as yours is not a valid string, remove ''
to make it a valid object literal, then you can iterate over the keys of the Object and check if it has the matching POSTCODE and if it has then return it's corresponding state.
删除''
和你是不是一个有效的字符串,删除''
,使之成为有效的对象文字,那么你就可以遍历对象的钥匙并检查是否有匹配的邮编,如果它再回到它的相应的状态。
var data = {
"1": {
"state": "VIC",
"postcode": "2600,2603,2605,2606"
},
"2": {
"state": "NSW",
"postcode": "2259,2264"
}
};
function getState(data, postcode){
for(var x in data){
if(data[x].postcode && data[x].postcode.split(",").indexOf(postcode.toString())!=-1) return data[x].state;
}
return "Not Found";
}
alert(getState(data, "2600"));
alert(getState(data, 2264));
You can directly do .indexOf
on the postcode, even without using .split(",")
. But, then, it will also match with ,2600
which should not be the case. So, use split
.
您可以直接.indexOf
对邮政编码进行操作,即使不使用.split(",")
. 但是,那么,它也会与 匹配,2600
这不应该是这种情况。所以,使用split
.
Use json[x].postcode
condition to make sure that postcode field exists in the object. Otherwise, it will give an error if it does not exist.
使用json[x].postcode
条件确保对象中存在邮政编码字段。否则,如果不存在就会报错。
回答by Anik Islam Abhi
Try like this
像这样尝试
var data = '{"1": { "state": "VIC","postcode": "2600,2603,2605,2606"}, "2": {"state": "NSW","postcode": "2259,2264"}}';
var jsObj = JSON.parse(data);
var find = "2600";
var values = Object.keys(jsObj).filter(function(x) {
return jsObj[x].postcode.indexOf(find) > -1;
}).map(function(x) {
return jsObj[x].state;
});
console.log(values.length > 0 ? values[0] : "not found");
回答by CoderPi
function findState(data, postcode) {
var postcode = postcode.toString()
for (var k in data) {
var postcodes = data[k].postcode.split(",")
if (postcodes.indexOf(postcode) != -1)
return data[k].state
}
}
// Demo Output
var data = '{"1":{"state":"VIC","postcode":"2600,2603,2605,2606"},"2":{"state":"NSW","postcode":"2259,2264"}}'
var dataObj = JSON.parse(data)
var state = findState(dataObj, 2600)
document.write(state)
回答by Rajesh
You can try something like this:
你可以尝试这样的事情:
function searchInObject(object, searchKey, searchValue) {
for (var i in object) {
if (object[i][searchKey].indexOf(searchValue) > -1) {
return object[i];
}
}
}
(function() {
var data = {
"1": {
"state": "VIC",
"postcode": "2600,2603,2605,2606"
},
"2": {
"state": "NSW",
"postcode": "2259,2264"
}
}
var pin = "2600";
var result = searchInObject(data, "postcode", pin);
console.log(result.state);
pin = "2259";
result = searchInObject(data, "postcode", pin);
console.log(result.state);
})()
回答by Poul Kruijt
Well now.. You are just asking us to help you with homework :) Good thing im in a good mood.
现在好了.. 你只是要求我们帮你做作业:) 好东西我心情很好。
First get a proper JSON string, and parse it into a object using JSON.parse
. Then iterate this object and split the postcode string and find the state!
首先获取一个正确的 JSON 字符串,然后使用JSON.parse
. 然后迭代这个对象并拆分邮政编码字符串并找到状态!
var data = ......
var resp = JSON.parse(data);
function getStateByPostcode(postcode) {
var state = "";
for(var i in resp) {
if(resp.hasOwnProperty(i)) {
var postcodes = resp[i]['postcode'].split(',');
if(postcodes.indexOf(postcode) !== -1) {
return resp[i]['state'];
}
}
}
return state;
}