在特定位置将JSON对象添加到另一个JSON对象 [英] Add JSON object to Another JSON Object at particular place

查看:193
本文介绍了在特定位置将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.

这篇关于在特定位置将JSON对象添加到另一个JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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