JavaScript-构建JSON对象

JavaScript-构建JSON对象,javascript,jquery,json,Javascript,Jquery,Json,我试图理解如何在JavaScript中构建JSON对象。这个JSON对象将被传递给JQuery ajax调用。目前,我正在硬编码JSON并进行JQuery调用,如下所示: $.ajax({ url: "/services/myService.svc/PostComment", type: "POST", contentType: "application/json; charset=utf-8", data: '{"comments":"test","priority":"1"}

我试图理解如何在JavaScript中构建JSON对象。这个JSON对象将被传递给JQuery ajax调用。目前,我正在硬编码JSON并进行JQuery调用,如下所示:

$.ajax({
  url: "/services/myService.svc/PostComment",
  type: "POST",
  contentType: "application/json; charset=utf-8",
  data: '{"comments":"test","priority":"1"}',
  dataType: "json",
  success: function (res) {
    alert("Thank you!");
  },
  error: function (req, msg, obj) {
    alert("There was an error");
  }
});        
var comments = $("#commentText").val();
var priority = $("#priority").val();
var json = { "comments":comments,"priority":priority };

$.ajax({
  url: "/services/myService.svc/PostComment",
  type: "POST",
  contentType: "application/json; charset=utf-8",
  data: json,
  dataType: "json",
  success: function (res) {
    alert("Thank you!");
  },
  error: function (req, msg, obj) {
    alert("There was an error");
  }
}); 
这种方法有效。但是,我需要动态构建JSON并将其传递给JQuery调用。但是,我不知道如何动态构建JSON对象。目前,我正在毫无运气地尝试以下内容:

var comments = $("#commentText").val();
var priority = $("#priority").val();
var json = { "comments":comments,"priority":priority };

$.ajax({
  url: "/services/myService.svc/PostComment",
  type: "POST",
  contentType: "application/json; charset=utf-8",
  data: json,
  dataType: "json",
  success: function (res) {
    alert("Thank you!");
  },
  error: function (req, msg, obj) {
    alert("There was an error");
  }
}); 
有人能告诉我我做错了什么吗?我注意到,在第二个版本中,我的服务甚至还没有实现

var comments = $("#commentText").val();
var priority = $("#priority").val();
var json = { "comments":comments,"priority":priority };

$.ajax({
  url: "/services/myService.svc/PostComment",
  type: "POST",
  contentType: "application/json; charset=utf-8",
  data: json,
  dataType: "json",
  success: function (res) {
    alert("Thank you!");
  },
  error: function (req, msg, obj) {
    alert("There was an error");
  }
}); 

谢谢你

你可能想看看。它有一个
stringify()
函数,我想它会完全满足您的需要。

这应该可以

var comments = $("#commentText").val();
var priority = $("#priority").val();
var json = { "comments":comments,"priority":priority };

$.ajax({
  url: "/services/myService.svc/PostComment",
  type: "POST",
  contentType: "application/json; charset=utf-8",
  data: json,
  dataType: "json",
  success: function (res) {
    alert("Thank you!");
  },
  error: function (req, msg, obj) {
    alert("There was an error");
  }
}); 
var json = { comments: "comments",priority: "priority" };
删除引号

var comments = $("#commentText").val();
var priority = $("#priority").val();
var json = { "comments":comments,"priority":priority };

$.ajax({
  url: "/services/myService.svc/PostComment",
  type: "POST",
  contentType: "application/json; charset=utf-8",
  data: json,
  dataType: "json",
  success: function (res) {
    alert("Thank you!");
  },
  error: function (req, msg, obj) {
    alert("There was an error");
  }
}); 
data: '{"comments":"test","priority":"1"}',
变成

var comments = $("#commentText").val();
var priority = $("#priority").val();
var json = { "comments":comments,"priority":priority };

$.ajax({
  url: "/services/myService.svc/PostComment",
  type: "POST",
  contentType: "application/json; charset=utf-8",
  data: json,
  dataType: "json",
  success: function (res) {
    alert("Thank you!");
  },
  error: function (req, msg, obj) {
    alert("There was an error");
  }
}); 
data: {"comments":"test","priority":"1"},
JSON是对象而不是字符串

这对我很有用

var comments = $("#commentText").val();
var priority = $("#priority").val();
var json = { "comments":comments,"priority":priority };

$.ajax({
  url: "/services/myService.svc/PostComment",
  type: "POST",
  contentType: "application/json; charset=utf-8",
  data: json,
  dataType: "json",
  success: function (res) {
    alert("Thank you!");
  },
  error: function (req, msg, obj) {
    alert("There was an error");
  }
}); 
var json = "{ 'comments': '" + *comments* +"','priority:' '" + *priority* +"' }";
斜体是变量。

您的代码:

var comments = $("#commentText").val();
var priority = $("#priority").val();
var json = { "comments":comments,"priority":priority };

$.ajax({
  url: "/services/myService.svc/PostComment",
  type: "POST",
  contentType: "application/json; charset=utf-8",
  data: json,
  dataType: "json",
  success: function (res) {
    alert("Thank you!");
  },
  error: function (req, msg, obj) {
    alert("There was an error");
  }
}); 
var comments = $("#commentText").val();
var priority = $("#priority").val();
var json = { "comments":comments,"priority":priority };
去掉引号(第3行):

var comments = $("#commentText").val();
var priority = $("#priority").val();
var json = { "comments":comments,"priority":priority };

$.ajax({
  url: "/services/myService.svc/PostComment",
  type: "POST",
  contentType: "application/json; charset=utf-8",
  data: json,
  dataType: "json",
  success: function (res) {
    alert("Thank you!");
  },
  error: function (req, msg, obj) {
    alert("There was an error");
  }
}); 

你有任何Javascript错误吗?很好的一点-试着在Firebug调试器打开的情况下运行它,看看你得到了什么。我没有收到任何Javascript错误。我的$.ajax调用中的错误事件处理程序正在被触发。好了,你看。。。您返回了什么错误消息?有帮助吗?啊!这是一个类型不匹配的问题。这是我最初的想法,但从他在示例中使用的代码来看,jQuery应该为您解决这个问题。@Justin Ethier:不,jQuery没有JSON序列化(只有反序列化)。jQuery对@user208662传递给它的数据对象做了一些完全不同的事情。@Crescent-Fresh:docs()有一个执行
数据的例子:({id:this.getAttribute('id')})
。我在我的应用程序中使用了相同的语法,它在没有stringify的情况下运行得很好(当然,这在其他地方仍然很有用)。@Mark,@Justin Ethier:同样,jQuery不使用JSON序列化。@Mark的示例和答案中传递的数据对象只是被路由到
jQuery.param()
(),这是一种完全不同的序列化方式。据我所知,在JSON中,所有字符串都需要双引号,所以“注释”和“优先级”。这是一个Javascript对象,所以如果您试图只显示一个Javascript对象,那么它是有效的。但是,对于JSON格式,字符串需要双引号。如果注释和优先级变量也包含双引号,会发生什么?这是失败的。这不是理想的解决方案。