如何获取json结构中键的单个特定值?

时间:2023-01-15 21:15:59

I am doing some exercise like:

我正在做一些运动,如:

var jsonres;
jsonres = JSON.stringify(jsonObjectArray);
alert(jsonvals); // getting the below json structure

jsonres = {
    "key01": [10, "Key01 Description"],
    "key02": [false, "It's a false value"],
    "key03": [null, "Testing Null"],
    "key04": ["tests", "Another Test Value"],
    "key05": [[25, 50], "Some testing values"]
}

But I need like:

但我需要:

jsonres = {
    "key01": 10,
    "key02": false,
    "key03": null,
    "key04": "tests",
    "key05": [25,50]
}

How can I get like above structure(means I need only single values, don't need the second values/multiple values for the respective keys) ? Please help me and thanks in advance.

我怎样才能获得上述结构(意味着我只需要单个值,不需要第二个值/相应键的多个值)?请帮助我,并提前感谢。

5 个解决方案

#1


1  

var jsonres = {
    "key01": [10, "Key01 Description"],
    "key02": [false, "It's a false value"],
    "key03": [null, "Testing Null"],
    "key04": ["tests", "Another Test Value"],
    "key05": [[25, 50], "Some testing values"]
}

for(var key in jsonres){
   if(jsonres.hasOwnProperty(key)){
      jsonres[key] = jsonres[key][0];
   }
}

console.log(jsonres)

https://jsfiddle.net/xd4nwc0m/

https://jsfiddle.net/xd4nwc0m/

#2


3  

Try

尝试

for(var key in jsonres) {
    jsonres[key] = jsonres[key][0];
}

Here's a fiddle https://jsfiddle.net/Lzb1dum3/

这是一个小提琴https://jsfiddle.net/Lzb1dum3/

#3


1  

Run this and see that it generates what you want:

运行它,看看它生成你想要的:

var jsonres = {
  "key01": [10, "Key01 Description"],
  "key02": [false, "It's a false value"],
  "key03": [null, "Testing Null"],
  "key04": ["tests", "Another Test Value"],
  "key05": [[25, 50], "Some testing values"]
} 

for (var key in jsonres) {
  jsonres[key] = jsonres[key][0];
  alert(jsonres[key]);
}

#4


1  

Just one line of code for iterating the keys and assigning:

只需一行代码来迭代键并分配:

var jsonres = {
    "key01": [10, "Key01 Description"],
    "key02": [false, "It's a false value"],
    "key03": [null, "Testing Null"],
    "key04": ["tests", "Another Test Value"],
    "key05": [[25, 50], "Some testing values"]
}

Object.keys(jsonres).forEach(function (k) { jsonres[k] = jsonres[k][0]; });
document.write('<pre>' + JSON.stringify(jsonres, 0, 4) + '</pre>');

#5


1  

try like this

试试这样

var editer = angular.module('editer', []);
function myCtrl($scope) {
$scope.jsonres = {
  "key01": [10, "Key01 Description"],
  "key02": [false, "It's a false value"],
  "key03": [null, "Testing Null"],
  "key04": ["tests", "Another Test Value"],
  "key05": [[25, 50], "Some testing values"]
} 

angular.forEach($scope.jsonres, function(value,key){
      $scope.jsonres[key] = value[0];
  });

console.log($scope.jsonres);
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="editer" ng-controller="myCtrl" class="container">
  
  <pre >{{jsonres|json}}</pre>
</div>

#1


1  

var jsonres = {
    "key01": [10, "Key01 Description"],
    "key02": [false, "It's a false value"],
    "key03": [null, "Testing Null"],
    "key04": ["tests", "Another Test Value"],
    "key05": [[25, 50], "Some testing values"]
}

for(var key in jsonres){
   if(jsonres.hasOwnProperty(key)){
      jsonres[key] = jsonres[key][0];
   }
}

console.log(jsonres)

https://jsfiddle.net/xd4nwc0m/

https://jsfiddle.net/xd4nwc0m/

#2


3  

Try

尝试

for(var key in jsonres) {
    jsonres[key] = jsonres[key][0];
}

Here's a fiddle https://jsfiddle.net/Lzb1dum3/

这是一个小提琴https://jsfiddle.net/Lzb1dum3/

#3


1  

Run this and see that it generates what you want:

运行它,看看它生成你想要的:

var jsonres = {
  "key01": [10, "Key01 Description"],
  "key02": [false, "It's a false value"],
  "key03": [null, "Testing Null"],
  "key04": ["tests", "Another Test Value"],
  "key05": [[25, 50], "Some testing values"]
} 

for (var key in jsonres) {
  jsonres[key] = jsonres[key][0];
  alert(jsonres[key]);
}

#4


1  

Just one line of code for iterating the keys and assigning:

只需一行代码来迭代键并分配:

var jsonres = {
    "key01": [10, "Key01 Description"],
    "key02": [false, "It's a false value"],
    "key03": [null, "Testing Null"],
    "key04": ["tests", "Another Test Value"],
    "key05": [[25, 50], "Some testing values"]
}

Object.keys(jsonres).forEach(function (k) { jsonres[k] = jsonres[k][0]; });
document.write('<pre>' + JSON.stringify(jsonres, 0, 4) + '</pre>');

#5


1  

try like this

试试这样

var editer = angular.module('editer', []);
function myCtrl($scope) {
$scope.jsonres = {
  "key01": [10, "Key01 Description"],
  "key02": [false, "It's a false value"],
  "key03": [null, "Testing Null"],
  "key04": ["tests", "Another Test Value"],
  "key05": [[25, 50], "Some testing values"]
} 

angular.forEach($scope.jsonres, function(value,key){
      $scope.jsonres[key] = value[0];
  });

console.log($scope.jsonres);
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="editer" ng-controller="myCtrl" class="container">
  
  <pre >{{jsonres|json}}</pre>
</div>