如何在数组中获取JSON对象?

时间:2021-09-07 21:17:12
var data =  '[{"type":"product","id":1,"label":"Size","placeholder":"Select Size","description":"","defaultValue"
:{"text":"Size30","price":"20"},"choices":[{"text":"Size30","price":"20","isSelected":"true"},{"text"
:"Size32","price":"22","isSelected":false},{"text":"Size34","price":"28","isSelected":false}],"conditionalLogic"
:""},{"type":"product","id":2,"label":"Color","placeholder":"Select Color","description":"","defaultValue"
:{"text":"Black","price":"10"},"choices":[{"text":"Black","price":"10","isSelected":"true"},{"text"
:"Green","price":"22","isSelected":false},{"text":"Red","price":"28","isSelected":false}],"conditionalLogic"
:""},{"type":"product","id":3,"label":"Rise","placeholder":"Select Rise","description":"","defaultValue"
:{"text":"Low","price":"8"},"choices":[{"text":"High","price":"12","isSelected":"true"},{"text"
:"Low","price":"8","isSelected":false}],"conditionalLogic"
:""}]';

Here I have posted my JSON data. I want to get all the defaultValue in JSON/Array format. My output should be like-

在这里,我发布了我的JSON数据。我想以JSON / Array格式获取所有defaultValue。我的输出应该像 -

 defaultValues:['Size30','Black','Low']

How to manage that in the foreach loop?

如何在foreach循环中管理它?

my code :

我的代码:

var otherSelectedOption;
angular.forEach(data, function(optionValue, optionKey) {
      if (optionValue.defaultValue.text) {
          otherSelectedOption = (optionValue.defaultValue.text);
      }
      selectedOption = {defaultValues: otherSelectedOption};
      console.log(selectedOption);          
});

1 个解决方案

#1


2  

Your JSON is not valid, since objects are not separated by comma ,

您的JSON无效,因为对象不是用逗号分隔的,

Suppose this is the JSON

假设这是JSON

var obj = '[{"type":"product","id":1,"label":"Size","placeholder":"Select Size","description":"","defaultValue"
:{"text":"Size30","price":"20"},"choices":[{"text":"Size30","price":"20","isSelected":"true"},{"text"
:"Size32","price":"22","isSelected":false},{"text":"Size34","price":"28","isSelected":false}],"conditionalLogic"
:""},{"type":"product","id":2,"label":"Color","placeholder":"Select Color","description":"","defaultValue"
:{"text":"Black","price":"10"},"choices":[{"text":"Black","price":"10","isSelected":"true"},{"text"
:"Green","price":"22","isSelected":false},{"text":"Red","price":"28","isSelected":false}],"conditionalLogic"
:""},{"type":"product","id":3,"label":"Rise","placeholder":"Select Rise","description":"","defaultValue"
:{"text":"Low","price":"8"},"choices":[{"text":"High","price":"12","isSelected":"true"},{"text"
:"Low","price":"8","isSelected":false}],"conditionalLogic"
:""}]';

try

尝试

var arr = JSON.parse(obj).map( function(item){
   return item.defaultValue;
});

#1


2  

Your JSON is not valid, since objects are not separated by comma ,

您的JSON无效,因为对象不是用逗号分隔的,

Suppose this is the JSON

假设这是JSON

var obj = '[{"type":"product","id":1,"label":"Size","placeholder":"Select Size","description":"","defaultValue"
:{"text":"Size30","price":"20"},"choices":[{"text":"Size30","price":"20","isSelected":"true"},{"text"
:"Size32","price":"22","isSelected":false},{"text":"Size34","price":"28","isSelected":false}],"conditionalLogic"
:""},{"type":"product","id":2,"label":"Color","placeholder":"Select Color","description":"","defaultValue"
:{"text":"Black","price":"10"},"choices":[{"text":"Black","price":"10","isSelected":"true"},{"text"
:"Green","price":"22","isSelected":false},{"text":"Red","price":"28","isSelected":false}],"conditionalLogic"
:""},{"type":"product","id":3,"label":"Rise","placeholder":"Select Rise","description":"","defaultValue"
:{"text":"Low","price":"8"},"choices":[{"text":"High","price":"12","isSelected":"true"},{"text"
:"Low","price":"8","isSelected":false}],"conditionalLogic"
:""}]';

try

尝试

var arr = JSON.parse(obj).map( function(item){
   return item.defaultValue;
});