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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 16:21:43  来源:igfitidea点击:

How to find value in json

javascriptjson

提问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 stateby postcode;

如何找到statepostcode;

if i search postcode 2600if 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 .indexOfon the postcode, even without using .split(","). But, then, it will also match with ,2600which should not be the case. So, use split.

您可以直接.indexOf对邮政编码进行操作,即使不使用.split(","). 但是,那么,它也会与 匹配,2600这不应该是这种情况。所以,使用split.

Use json[x].postcodecondition 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");

JSFIDDLE

JSFIDDLE

回答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;
}