在特定位置将JSON对象添加到另一个JSON对象

问题描述:

我有两个JSON对象:

I have two JSON Objects:

var JSonData = {"li_ZPI":{},"li_ZIN":{"li_ISD":{},"li_AAH":{"li_AAD":{}},"li_EAH":{"li_EAD":{}},"li_REG":{},"li_PSC":{},"li_IMC":{},"li_GSP":{},"li_IES":{}}};

var additionalJSonData = {"li_AAH_1":{"li_AAD_1":{}}};

我想在JSONData "li_AAH"对象之后添加additionalJSonData对象.

I want to add additionalJSonData object after JSONData "li_AAH" Object.

问题是在之后没有.这些"JSON"对象(实际上是Javascript对象,JSON是可以转换为JS对象的文本字符串)是哈希,而不是数组.哈希没有顺序-即,

The problem is that there's no after. These "JSON" objects (Javascript objects, really, JSON would be text strings that could convert to JS objects) are hashes, not arrays. Hashes have no order - that is,

for ( var f in JSonData ) {
}

没有规定各种键("li_ZPI"等)将以任何特定顺序显示,甚至每次都以相同顺序显示.

there's no rule that the various keys ("li_ZPI", etc.) would show up in any particular order, or even the same order each time.

因此您当然可以添加新项目:

So you can certainly add the new item:

JSonData["li_AAH_1"] = {"li_AAD_1":{}};

但是不要期望最终的JSON字符串按照您希望的方式排序.

but don't expect the eventual JSON string to be ordered the way you're hoping.