如何将记录添加到json jquery?

时间:2018-05-17 06:00:59

标签: jquery json

我有json架构:

var data_json   {
"protocol": {
    "protocol_descr": {
        "protocol_name": "test"
    },
    "protocol_body": {
        "group": {
            "group_name": "test group",
            "position_info": {
                "position_name": "1",
                "position_type": "tags",
                "position_value": "some,value",
                "data-free":"false",
                "data-type":"false",
                "data-optional":"false"
            }
        }
    }
}
}

如何将另外一个组添加到“protocol_body”? 我尝试使用下一个构造 - data_json.protocol.protocol_body.push(),但它不起作用。

3 个答案:

答案 0 :(得分:0)

您可以直接将具有赋值对象的对象分配给json对象的键值

检查以下工作代码段



var data_json  = {
"protocol": {
    "protocol_descr": {
        "protocol_name": "test"
    },
    "protocol_body": {
        "group": {
            "group_name": "test group",
            "position_info": {
                "position_name": "1",
                "position_type": "tags",
                "position_value": "some,value",
                "data-free":"false",
                "data-type":"false",
                "data-optional":"false"
            }
        }
    }
}
}
data_json.protocol.protocol_body.grop2 = {
  "group_name": "test group",
        "position_info": {
            "position_name": "1",
            "position_type": "tags",
            "position_value": "some,value",
            "data-free":"false",
            "data-type":"false",
            "data-optional":"false"
        }
}
console.log(data_json);




答案 1 :(得分:0)

您应该将group属性更改为数组类型而不是对象,以便您可以在该属性中推送更多组。像这样:



var data_json = {
  "protocol": {
      "protocol_descr": {
        "protocol_name": "test"
      },
      "protocol_body": {
        "group": [{
            "group_name": "test group",
            "position_info": {
                "position_name": "1",
                "position_type": "tags",
                "position_value": "some,value",
                "data-free":"false",
                "data-type":"false",
                "data-optional":"false"
            }
        }]
    }
  }
};

var newGroup = {
   "group_name": "test group2",
    "position_info": {
        "position_name": "2",
        "position_type": "tags2",
        "position_value": "some,value",
        "data-free":"false",
        "data-type":"false",
        "data-optional":"false"
    }
};

data_json.protocol.protocol_body.group.push(newGroup);
console.log(data_json);




答案 2 :(得分:0)

在您的数据中“protocol_body”不是一个数组,因为它不支持push。 你可以用它。

// Code goes here

function cntryController($scope) {
  
  
  $scope.LoadSessionData=function(val)
  {
    
     console.log(val);
    
   
    
  };
  
      $scope.changed_configs = []; 
          $scope.configs = [{
                   id: "1",
                   name: "config1"
                   },
                   {
                   id: "2",
                   name: "config2"
                   }
          ];

        $scope.addConfig = function () {
          var config = {
              "id": "",
              "value": ""
          };
          $scope.changed_configs.push(config);
          console.log(config);
          console.log(JSON.stringify($scope.changed_configs));
        }
  
    
  $scope.test = function(data, index){
    console.log(data)
    var newArray = $scope.configs.filter(function (config) {
  return config.name == data;
});
    console.log(newArray)
    if(newArray.length){
      var new_changed_config = $scope.changed_configs[index];
      new_changed_config.id = newArray[0].id;
    }
  }
  
  
  
  
  
}