JavaScript中的JSON转换 [英] JSON conversion in javascript
问题描述
我正在尝试将多数组变量字符串化为Javascript中的JSON字符串.
I'm trying to stringify a multi-array variable into a JSON string in Javascript. The
//i'm using functions from http://www.json.org/json2.js
var info = new Array(max);
for (var i=0; i<max; i++) {
var coordinate = [25 , 32];
info[i] = coordinate;
}
var result = JSON.stringify(info);
但是结果根本看起来不像JSON字符串.我在这里做什么错了?
But result doesn't look like a JSON string at all. What am I doing wrong here?
推荐答案
您和这个问题中的许多人都对JSON规范感到困惑.您拥有的字符串是有效的JSON数组.
You, and many in this question, are confused about the JSON specification. The string you have is a valid JSON array.
来自json.org
JSON建立在两个结构上:
JSON is built on two structures:
- 名称/值对的集合.在各种语言中,这是 实现为一个对象,记录, 结构,字典,哈希表, 键控列表或关联数组.
- 值的有序列表.在大多数语言中,这被实现为 数组,向量,列表或 顺序.
- A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
- An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.
您的示例与第二个结构匹配-值的有序列表.
Your example matches the second structure - the ordered list of values.
也来自json.org:
一个数组是一个有序集合 价值观.数组以[(左 并以](右)结尾 括号).值之间用分隔, (逗号).
An array is an ordered collection of values. An array begins with [ (left bracket) and ends with ] (right bracket). Values are separated by , (comma).
一个值可以是一个双精度字符串 引号或数字,或对或错 或null,或者对象或数组. 这些结构可以嵌套.
A value can be a string in double quotes, or a number, or true or false or null, or an object or an array. These structures can be nested.
没有太多的想像力.您在那里有一个有效的JSON数组.玩得开心.只是要烦人的彻底:
Doesn't leave much to the imagination. You've got a valid JSON array there. Have fun with it. But just to be annoyingly thorough:
来自 RFC
From the RFC
RFC 4627,第2部分
RFC 4627, Section 2
2)JSON语法
JSON文本是一系列 令牌.令牌集包括 六个结构字符,字符串 数字和三个文字名称.
A JSON text is a sequence of tokens. The set of tokens includes six structural characters, strings, numbers, and three literal names.
JSON文本是序列化的对象 或数组.
A JSON text is a serialized object or array.
JSON-text = object / array
这篇关于JavaScript中的JSON转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!