如何使用重复键动态生成JSON对象? [英] How to generate a JSON object dynamically with duplicate keys?

查看:214
本文介绍了如何使用重复键动态生成JSON对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

我真的很感谢你的任何帮助!

I would realy appreciate any help from you!

推荐答案

从我所看到的, {a:b,a:c} 根据 RFC 4627 ,实际上有效 JSON。

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


对象结构表示为一对大括号
,包含零个或多个名称/值对(或成员)。名称是
字符串。每个名称后面都有一个冒号,将名称
与值分开。单个逗号将值与后续
名称分隔开。对象应该中的名称是唯一的。

...其中SHOULD表示:

...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解码器可能会以不同的方式和/或以不可分割的方式处理这种情况。查看规范对解析器的要求:

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文本转换为另一种表示形式。
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.

    你想要在生产代码中做的最后一件事就是依靠奇怪的边案例。所以你要做的最后一件事是行使你的权利,形成名义上合法但实际上无用的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天全站免登陆