在JavaScript中创建JSON [英] Creating JSON in javascript
问题描述
如何在javascript中将此数据编码为JSON?
How should I encode this data as JSON in javascript?
我使用javascript获取照片上任意数量的标签".每个标签都有以下形式的名字和姓氏:
I use javascript to get an arbitrary number of 'tags' on photos. Each tag has a firstname and a lastname in this form:
firstname = 'john';
lastname = 'doe';
firstname = 'jane';
lastname = 'smith';
我想将此数据编码为JSON,使用Ajax将其发送到我的服务器,并在PHP中对其进行解码.
I want to encode this data as JSON, send it to my server with Ajax, and decode it in PHP.
我的想法是创建一个多维数组.有更好的方法吗?
My thought was to create a multidimensional array. Is there a better way to do this?
JSON.stringify()的输出为[{\"firstname\":\"John\",\"lastname\":\"Doe\"},{\"firstname\":\"Jane\",\"lastname\":\"Smith\"}]
.如何让JSON.stringify()不能转义所有引号?
The output of JSON.stringify() is [{\"firstname\":\"John\",\"lastname\":\"Doe\"},{\"firstname\":\"Jane\",\"lastname\":\"Smith\"}]
. How can I have JSON.stringify() not escape all of the quotes?
推荐答案
JSON受JavaScript对象语法的启发,因此您所需要做的就是创建对象数组:
JSON is inspired by JavaScript's object syntax, so all you need to do is create an array of objects:
var data = [
{
firstname: 'john',
lastname: 'doe'
},
{
firstname: 'jane',
lastname: 'smith'
}
]
var json = JSON.stringify( data ); // send this object to server
这篇关于在JavaScript中创建JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!