JavaScript - 构建 JSON 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2524425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:45:01  来源:igfitidea点击:

JavaScript - Building JSON object

javascriptjqueryjson

提问by user208662

I'm trying to understand how to build a JSON object in JavaScript. This JSON object will get passed to a JQuery ajax call. Currently, I'm hard-coding my JSON and making my JQuery call as shown here:

我试图了解如何在 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");
  }
});        

This approach works. But, I need to dynamically build my JSON and pass it onto the JQuery call. However, I cannot figure out how to dynamically build the JSON object. Currently, I'm trying the following without any luck:

这种方法有效。但是,我需要动态构建我的 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");
  }
}); 

Can someone please tell me what I am doing wrong? I noticed that with the second version, my service is not even getting reached.

有人可以告诉我我做错了什么吗?我注意到,在第二个版本中,我的服务甚至没有达到。

Thank you

谢谢

采纳答案by LBushkin

You may want to look at the JSON JavaScript library. It has a stringify()function which I think will do exactly what you need.

您可能需要查看JSON JavaScript 库。它有一个stringify()功能,我认为它可以完全满足您的需求。

回答by Shaggy

Your code:

您的代码:

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

Take out the quotes ( line 3 ):

取出引号(第 3 行):

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

回答by plodder

Remove the quotes

删除引号

data: '{"comments":"test","priority":"1"}',

becomes

变成

data: {"comments":"test","priority":"1"},

JSONs are objects not strings.

JSON 是对象而不是字符串。

回答by Arindam Nayak

this works for me.

这对我有用。

var json = "{ 'comments': '" + *comments* +"','priority:' '" + *priority* +"' }";

italics are the variables.

斜体是变量。

回答by Rigobert Song

This should work

这应该工作

var json = { comments: "comments",priority: "priority" };