如何使用javascript和jquery创建JSON数据

时间:2015-06-21 08:11:51

标签: javascript jquery json angularjs

我想使用以下格式动态创建JSON对象:

d

我在控制器中获取数据,如下所示。

{
    "deleteId":[1,2,3],
    "pointId":[1,2,3],
    "update":[
        {
            "what":"mission",
            "id":1,
            "value":"adsda"
        },
        {
            "what":"mission",
            "id":2,
            "value":"sadjajks"
        },
        {
            "what":"point",
            "id":3,
            "value":"asjdjh"
        }
    ]
}

我努力制作上述格式的JSON数据,edit_vision_mission.controller('edit_vision_missionCtrl',['$scope','$http', 'getMissionDataService', function($scope, $http, getMissionDataService) { $scope.visiontext = "Here is the content of vision"; getMissionDataService.getMissionData().success(function(response){ $scope.missions = response; $scope.len = $scope.missions.length; }); var jsonData = {}; jsonData.deleteId = []; jsonData.pointId = []; jsonData.update = []; $scope.deletemission = function(missionid){ jsonData.deleteId.push(missionid); alert("deleted " + jsonData.deleteId); }; $scope.deletemissionpoints = function(missionpointid){ jsonData.pointId.push(missionpointid); console.log(missionpointid); alert("deleted " + jsonData.pointId); }; $scope.updatemission = function(missionid, info){ var updateinfo = {"what" : "mission", "id" : missionid, "value" : info}; jsonData.update.push(updateinfo); alert("updated " + jsonData.update); }; $scope.updatemissionpoints = function(missionpointid, info){ var updateinfo = {"what" : "point", "id" : missionpointid, "value" : info}; jsonData.update.push(updateinfo); alert("updated " + jsonData.update); }; }]); 未在警报中显示任何内容。

我需要有关如何制作上述格式的JSON的帮助。

我也想知道在前端测试JSON数据的方法吗?

1 个答案:

答案 0 :(得分:1)

jsonData是一个对象。因此,您的提醒应显示"updated" [ object Object ]

上述所有方法似乎都很好。

要以更干净的方式查看对象,请使用chrome开发人员工具。最后一个名为 console 的标签。您可以通过右键单击页面打开开发人员工具,然后选择检查元素

您需要将代码更新为以下内容,

执行alert( "updated" + jsonData.update )

而不是执行此操作console.log( jsonData.update )

console.log()中混合使用两种类型并不允许它进行解析。

E.g。来自您的上述样本&代码。

var sample = '{"deleteId":[1,2,3],"pointId":[1,2,3],"update":[ {"what":"mission","id":1,"value":"adsda" }, {"what":"mission","id":2,"value":"sadjajks" }, {"what":"point","id":3,"value":"asjdjh" }]}';

var jsonData = {};

jsonData.deleteId = [];
jsonData.pointId = [];
jsonData.update = [];

var deletemission = function(missionid){
    jsonData.deleteId.push(missionid);
    console.log (  " deleted " );
    console.log ( jsonData.deleteId );
};

var deletemissionpoints = function(missionpointid){
    jsonData.pointId.push(missionpointid);
    console.log (  " deleted " );
    console.log ( jsonData.pointId );
};

var updatemission = function(missionid, info){
   var updateinfo = {"what" : "mission", "id" : missionid, "value" : info};
   jsonData.update.push(updateinfo);
   console.log (  " updated " );
   console.log ( jsonData.update );
};

var updatemissionpoints = function(missionpointid, info){
    var updateinfo = {"what" : "point", "id" : missionpointid, "value" : info};
   jsonData.update.push(updateinfo);
   console.log (  " updated " );
   console.log ( jsonData.update );
};

sample = JSON.parse( sample );

for( var i = 0 ; i < sample.deleteId.length; i++ ) {
  deletemission( sample.deleteId[i] );
}

for( i = 0 ; i < sample.pointId.length; i++ ) {
  deletemissionpoints( sample.pointId[i] );
}

for( i = 0 ; i < sample.update.length; i++ ) {

  if ( sample.update[i].what == "mission") {
    updatemission(  sample.update[i].id , sample.update[i].value )
  } else {
    updatemissionpoints(  sample.update[i].id , sample.update[i].value )
  }

}

console.log( jsonData );