[英]Building a JSON string using javascript/jQuery
I'm using the $.post() function from jQuery to make a Ajax call with a JSON string. 我正在使用jQuery的$ .post()函数使用JSON字符串进行Ajax调用。 The call looks like this: 呼叫看起来像这样:
$.post(
urlVar,
jsonVar,
function(data){
//do stuff
},
'json'
)
.complete(function(){
//do other stuff
});
To create jsonVar I'm using this code 要创建jsonVar,我正在使用此代码
var1 = {};
var1.id = fooId;
var1.amount = fooAmount;
var1.zoom = fooZoom;
jsonVar = JSON.stringify(var1);
To make the call work, jsonVar should look like this 为了使呼叫正常工作, jsonVar应该看起来像这样
{id:fooId, amount:fooAmount, zoom:fooZoom}
but it looks like this 但看起来像这样
{"id":fooId, "amount":fooAmount, "zoom":fooZoom}
Now my code will not work, because of the double quotes. 现在,由于双引号,我的代码无法使用。 I couldn't figure out how to get rid of those. 我不知道该如何摆脱这些。 Can anyone help me out? 谁能帮我吗?
IMPORTANT: 重要:
the code does work if I put the $.post()
function like this: 如果我像这样放置$.post()
函数,则代码可以正常工作:
$.post(
urlVar,
{id: fooId, amount: fooAmount, zoom: fooZoom},
function(data){
//do stuff
},
'json'
)
.complete(function(){
//do other stuff
});
The JSON specification states that the keys must have double-quotes. JSON规范指出,键必须带有双引号。
What do you mean your code won't work because of double quotes? 您是什么意思,因为双引号导致代码无法正常工作? Parse the JSON back into an object using JSON.parse ; 使用JSON.parse将JSON解析回一个对象; which is built-in to many modern browsers or you can shim it using the json2 library. 它内置于许多现代浏览器中,或者您可以使用json2库对其进行填充。
Given this code: 给出以下代码:
var fooId = 'foodyfood';
var fooAmount = 10.20;
var fooZoom = 'zoomer';
var var2 = {};
var var1 = {};
var1.id = fooId;
var1.amount = fooAmount;
var2.zoom = fooZoom;
jsonVar = JSON.stringify(var1);
$('#showme').text(jsonVar);
the value shown in the showme would be: 在showme中显示的值为:
{"id":"foodyfood","amount":10.2}
so your example appears to be flawed and actually is not JSON standard which specifies the double quotes. 因此您的示例似乎有缺陷,实际上不是指定双引号的JSON标准。
EDIT: now your use of the post is the equivelent of: 编辑:现在,您对帖子的使用等同于:
$.ajax({
type: 'POST',
url: url,
data: data,
success: success,
dataType: dataType
});
except that it handles the object as objects in your second example after your edit. 除了在编辑后的第二个示例中将对象作为对象处理之外。
see the example in the documentation here: http://api.jquery.com/jQuery.post/ 请参阅以下文档中的示例: http : //api.jquery.com/jQuery.post/
with this form: 使用以下形式:
$.post("test.php", { name: "John", time: "2pm" }, function(data) {
alert("Data Loaded: " + data);
});
so if you have: 因此,如果您有:
var mytest = { name: "John", time: "2pm" };
it becomes: on the stringify 它变成:在串上
{"name":"John","time":"2pm"}
see working example here: http://jsfiddle.net/v4NHv/ 在此处查看工作示例: http : //jsfiddle.net/v4NHv/
Change the post
call like this 像这样更改post
通话
$.post(
urlVar,
{var1:jsonVar},
function(data){
//do stuff
},
'json'
)
.complete(function(){
//do other stuff
});
And then of course you will need to make a little tweek to your receiving app 然后,您当然需要花一些时间来处理接收应用程序
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.