js A Simple Method for Obtaining value Value Corresponding to key in json

  • 2021-08-03 08:21:58
  • OfStack

Examples are as follows:


<script type="text/javascript"> 
 getJson('age'); 
 
function getJson(key){ 
 var jsonObj={"name":" Fu Hongxue ","age":"24","profession":" Assassin "}; 
 
 
 //1 , using eval Method   
 var eValue=eval('jsonObj.'+key); 
 alert(eValue); 
 
 
 //2 , traversal Json String to get its properties  
 for(var item in jsonObj){ 
  if(item==key){ //item  Denote Json Attributes in the string, such as 'name' 
   var jValue=jsonObj[item];//key Corresponding to value 
   alert(jValue); 
  } 
 } 
 
 
 //3 , direct access  
 alert(jsonObj[''+key+'']); 
} 
</script> 

The value corresponding to key in json can be obtained by the above three methods

Obtain key and value values of json data in interface by js mode

Everyone in the actual operation of the interface inevitably encountered problems, presumably for some small white to get inside the data is also very headache, then I will combine the actual situation of the interface to teach you how to get the corresponding data inside

Interface data such as: (data is analog data, json format)


{
  "month": {
    "monthly": ["2018 Year 4 Month ", "2018 Year 5 Month "]
  },
  "nameAndRateOperation": {
    " Jinan Huayang carbon co., ltd ": ["91.67", "91.67"],
    " Jinan Wanrui Carbon Co., Ltd ": ["84", "84"],
    " Jinan Longshan carbon co., ltd ": ["85.71", "85.71"],
    " Dezhou O 'Lane Yongxing Carbon Co., Ltd. ": ["70", "80"],
    " Dezhou Yongxing carbon co., ltd ": ["90", "90"],
    " Dezhou ganhai carbon co., ltd ": ["70", "75"],
    " Jinan Haichuan Investment Group Co., Ltd ": ["81.25", "81.25"],
    " Jinan HSBC Carbon Co., Ltd ": ["86", "86"],
    " Shandong jinlida carbon co., ltd ": ["86.68", "80"],
    " Jinan Aohai Carbon Co., Ltd ": ["85", "85"]
  },
  "nameAndYield": {
    " Jinan Huayang carbon co., ltd ": ["1.2", "1.2"],
    " Jinan Wanrui Carbon Co., Ltd ": ["4", "4"],
    " Jinan Longshan carbon co., ltd ": ["2.4", "2.4"],
    " Dezhou O 'Lane Yongxing Carbon Co., Ltd. ": ["0.8", "1"],
    " Dezhou Yongxing carbon co., ltd ": ["0.67", "0.5"],
    " Dezhou ganhai carbon co., ltd ": ["0.2", "0.25"],
    " Jinan Haichuan Investment Group Co., Ltd ": ["1.6", "1.6"],
    " Jinan HSBC Carbon Co., Ltd ": ["0.8", "0.8"],
    " Shandong jinlida carbon co., ltd ": ["2", "2.5"],
    " Jinan Aohai Carbon Co., Ltd ": ["4.33", "4.5"]
  },
  "nameAndDateTime": {
    " Jinan Huayang carbon co., ltd ": ["2018 Year 4 Month ", "2018 Year 5 Month "],
    " Jinan Wanrui Carbon Co., Ltd ": ["2018 Year 4 Month ", "2018 Year 5 Month "],
    " Jinan Longshan carbon co., ltd ": ["2018 Year 4 Month ", "2018 Year 5 Month "],
    " Dezhou O 'Lane Yongxing Carbon Co., Ltd. ": ["2018 Year 4 Month ", "2018 Year 5 Month "],
    " Dezhou Yongxing carbon co., ltd ": ["2018 Year 4 Month ", "2018 Year 5 Month "],
    " Dezhou ganhai carbon co., ltd ": ["2018 Year 4 Month ", "2018 Year 5 Month "],
    " Jinan Haichuan Investment Group Co., Ltd ": ["2018 Year 4 Month ", "2018 Year 5 Month "],
    " Jinan HSBC Carbon Co., Ltd ": ["2018 Year 4 Month ", "2018 Year 5 Month "],
    " Shandong jinlida carbon co., ltd ": ["2018 Year 4 Month ", "2018 Year 5 Month "],
    " Jinan Aohai Carbon Co., Ltd ": ["2018 Year 4 Month ", "2018 Year 5 Month "]
  },
  "regionAndCompany": {
    " East China ": [" Dezhou O 'Lane Yongxing Carbon Co., Ltd. ", " Dezhou ganhai carbon co., ltd ", " Dezhou Yongxing carbon co., ltd ", " Shandong jinlida carbon co., ltd ", " Jinan Aohai Carbon Co., Ltd ", " Jinan Haichuan Investment Group Co., Ltd ", " Jinan Huayang carbon co., ltd ", " Jinan HSBC Carbon Co., Ltd ", " Jinan Longshan carbon co., ltd ", " Jinan Wanrui Carbon Co., Ltd "]
  },
  "nameAndCapacity": {
    " Jinan Huayang carbon co., ltd ": ["6", "6"],
    " Jinan Wanrui Carbon Co., Ltd ": ["5", "5"],
    " Jinan Longshan carbon co., ltd ": ["7", "7"],
    " Dezhou O 'Lane Yongxing Carbon Co., Ltd. ": ["30", "30"],
    " Dezhou Yongxing carbon co., ltd ": ["20", "20"],
    " Dezhou ganhai carbon co., ltd ": ["20", "20"],
    " Jinan Haichuan Investment Group Co., Ltd ": ["8", "8"],
    " Jinan HSBC Carbon Co., Ltd ": ["10", "10"],
    " Shandong jinlida carbon co., ltd ": ["15", "15"],
    " Jinan Aohai Carbon Co., Ltd ": ["10", "10"]
  },
  "storageWithCompany": {
    " Jinan Huayang carbon co., ltd ": ["0.8", "0.8"],
    " Jinan Wanrui Carbon Co., Ltd ": ["3.2", "3.2"],
    " Jinan Longshan carbon co., ltd ": ["2.2", "2.2"],
    " Dezhou O 'Lane Yongxing Carbon Co., Ltd. ": ["1.2", "1.2"],
    " Dezhou Yongxing carbon co., ltd ": ["0.5", "0.5"],
    " Dezhou ganhai carbon co., ltd ": ["0.15", "0.15"],
    " Jinan Haichuan Investment Group Co., Ltd ": ["1", "1"],
    " Jinan HSBC Carbon Co., Ltd ": ["0.5", "1"],
    " Shandong jinlida carbon co., ltd ": ["1.5", "1.5"],
    " Jinan Aohai Carbon Co., Ltd ": ["2.5", "2.5"]
  }
}

json Data 

Get code


// Take out all the areas : South China and North China ....
            for (var key in data.regionAndCompany) {
              region.push(key);
            }
            // console.log(region)
            
            // The array of warehouses fetched from the interface 
            for (key in data.storageWithCompany) {
              storageWithCompany.push(data.storageWithCompany[key])
            }
            // console.log(storageWithCompany)

            // // Array of capacity taken out by interface 
            // for(key in data.nameAndCapacity){
            //   capacity.push(data.nameAndCapacity[key])
            // }
            // // console.log(capacity)

            // An array of operating rates fetched from the interface 
            for (key in data.nameAndRateOperation) {
              rateOperation.push(data.nameAndRateOperation[key])
            }
            // console.log(rateOperation)

            // All company names 
            for (key in data.nameAndRateOperation) {
              companyRateOperation.push(key);
            }
            // console.log(companyRateOperation)
            
            // Total time data ,2 Dimensional array 
            for (var key in data.nameAndDateTime) {
              date.push(data.nameAndDateTime[key])
            }
            // console.log(date)
            
            // Total yield array ,2 Dimension , The output of each company is 1 Array of numbers 
            for (var key in data.nameAndYield) {
              yield.push(data.nameAndYield[key])
            }
            // console.log(yield)

The way to put it on the Internet again

Method of obtaining key value in object type in js

I recently encountered a problem:

var obj = {"name1": "Zhang 3", "name2": "Li 4"};
var key = "name1";
var value = obj. key; //Got "undefined"
value = obj. name1; //Got "Zhang 3"

Actually, I want to dynamically assign a value to key, and then get the right value for key. But this practice doesn't work. obj. key will look for the value corresponding to "key" under obj, but of course it can't be found.
So, I thought of the method of traversing object properties in js:


function printObject(obj){
	//obj = {"cid":"C0","ctext":" District and county "};
	var temp = "";
	for(var i in obj){// Use javascript Adj. for/in Loop through the properties of an object 
		temp += i+":"+obj[i]+"\n";
	}
	alert(temp);// Results: cid:C0 \n ctext: District and county 
}

In this way, you can clearly know what key and value of one object in js are respectively.
Back to the question just now, how to dynamically assign values to key, and then get the corresponding value in the way of obj. key?
In fact, there is a hint in the above printObject, that is, with obj [key], key can be dynamic, thus solving the problems I raised above.
Finally, there is another method that can be used, that is, eval ("obj." + key).

Summary:

In js, there are two ways to get the corresponding value in an object according to the dynamic key:

1. var key = "name1";var value = obj[key];
2. var key = "name1";var value = eval("obj."+key);


Related articles: