必要性在JSON对象重复键 [英] Necessity for duplicate keys in JSON object

查看:156
本文介绍了必要性在JSON对象重复键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这听起​​来不可能的,但我的老板告诉我,我必须发送一个JSON在一个AJAX调用后用jQuery必须具有重复键。问题是,如果我写的东西是这样的:

I know this will sound impossible but my boss told me I MUST send a JSON over an AJAX post call with jQuery that MUST HAVE DUPLICATE KEYS. the problem is that if I write something like this:

$.post("someurl", {
     "key1" : "value1",
     "key2" : "value2",
     "key2" : "value3",
     "key2" : "value4",
     "key3" : "value5"
});

,jQuery将请求发送作为

, jQuery will send the request as

someurl?key1=value1&key2=value4&key3=value5

这一切都是因为Javascript的覆盖具有相同名称的属性。 JSON对象是动态生成的,我不允许使​​用阵列它。谁能告诉我,我怎么可能产生的JSON对象dinamicaly和重复键?

all this because Javascript overwrites properties that have the same name. The JSON object is generated dynamically and I am NOT ALLOWED to use arrays in it. Can someone tell me how could I generate the JSON object dinamicaly and with duplicate keys?

我会从你真的AP preciate任何帮助!

I would realy appreciate any help from you!

推荐答案

这是我所看到的, {一:B,A:C} 实际上的有效的根据 RFC JSON 4627

From what I can see, {"a": "b", "a": "c"} actually is valid JSON according to RFC 4627.

这是对象结构被重新psented为一对大括号$ P $      周边零个或多个名称/值对(或成员)。一个名字是一个      串。一个冒号就是每一个名字后面,分离名称      从值。一个逗号从以下分隔值      名称。一个对象中的名称应该是唯一的。

An object structure is represented as a pair of curly brackets surrounding zero or more name/value pairs (or members). A name is a string. A single colon comes after each name, separating the name from the value. A single comma separates a value from a following name. The names within an object SHOULD be unique.

...应该在哪里的意思是:

...where SHOULD means:

3。应该。这个词,或形容词建议,意味着有      可能存在的特殊情况下忽略一个正当的理由      特定的项目,但全面影响必须被理解和      选择不同的做法之前,请仔细权衡。

3. SHOULD. This word, or the adjective "RECOMMENDED", mean that there may exist valid reasons in particular circumstances to ignore a particular item, but the full implications must be understood and carefully weighed before choosing a different course.

所以是的,基本上你的可以的做到这一点,是的法律的,但它也是一个坏主意。不同的JSON德codeRS可能会有所不同和/或undesiderable的方式处理这种情况。看看哪些规范要求解析器:

So yeah, basically you can do that, it is legal, but it's also a bad idea. Different JSON decoders will probably handle this situation differently and/or in undesiderable ways. Look at what the spec requires of parsers:

一个JSON解析器将一个JSON文本转换成另一种重presentation。一个      JSON解析器必须接受符合JSON语法的所有文本。      一个JSON解析器的可以接受非JSON的形式或扩展。

A JSON parser transforms a JSON text into another representation. A JSON parser MUST accept all texts that conform to the JSON grammar. A JSON parser MAY accept non-JSON forms or extensions.

这是实施可以在文本的大小设置的限制,它      接受。一个执行可以在最大深度设限      嵌套。一个实现的可以上的数字范围设置的限制。      一个执行可以的长度和字符内容设限      的字符串。

An implementation may set limits on the size of texts that it accepts. An implementation may set limits on the maximum depth of nesting. An implementation may set limits on the range of numbers. An implementation may set limits on the length and character contents of strings.

...但实施并不有无的以三立处理这种情况。例如:

...but an implementation doesn't have to handle situations like this sanely. For example:

# Python 2.7
>>> import json
>>> json.JSONDecoder().decode('{"a": "b", "a": "c"}')
`{u'a': u'c'}`
# Chrome 32
> JSON.parse('{"a": "b", "a": "c"}')
Object {a: "c"}

...等实现可以合法地给你(在Python符号):

...and other implementations may legally give you (in Python notation):

{"a": "b"}

  • [("a", "b"), ("a", "c")]

  • [("a", ["b", "c"])]

  • []

  • 42

  • "your JSON is bad and you should feel bad"

  • ...或者只是好老鼻守护进程的。从字面上的唯一合法的事情,一个JSON解析器做的,是引发一个例外。

    ...or just good old nasal daemons. Literally the only illegal thing for a JSON parser to do here is raise an exception.

    您想在您的生​​产code的最后一件事是依靠怪异的一面案件。所以,你想要做的最后一件事是行使您的权利,形成名义上是合法的,但实际上是没有用的JSON。如果你想做到这一点,你必须手工做到这一点 - 建立自己的抽象语法树,你自己的解析器,你自己的发电机,发电机任何人谁可能要消耗你的数据...

    The last thing you want to do in your production code is to rely on weird side cases. So the last thing you want to do is exercise your right to form nominally legal but practically useless JSON. If you want to do that, you'll have to do it by hand - build your own abstract syntax trees, your own parsers, your own generators, generators for anybody who might want to consume your data...

    这篇关于必要性在JSON对象重复键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆