简体   繁体   English

如何在 json 文件中编辑和添加嵌套数据?

[英]How can I edit and add nested data in json file?

This was just to test though but basically what it does is when it receives a message(message would be like abc/xyz/pq=yo) then makes a out of it like:- {"root":[{"abc":[{"xyz":[{"pq":"yo"}]}]}]} but whet I want is for example there exists a json already and if someone wants to add value like abc/xyz/lm=ayo then if it doesn't exist then it will add that to the existing json.这只是为了测试,但基本上它的作用是当它接收到一条消息时(消息就像 abc/xyz/pq=yo),然后像这样:- {"root":[{"abc":[{"xyz":[{"pq":"yo"}]}]}]}但是我想要的是,例如已经存在一个 json,如果有人想添加像 abc/xyz/lm=ayo 这样的值如果它不存在,那么它会将其添加到现有的 json 中。 and if someone wants to update something like abc/xyz/pq=ayo then it will update it.如果有人想更新像 abc/xyz/pq=ayo 这样的东西,它就会更新它。 That is if there exists a child then it updates a value else it creates it.也就是说,如果存在一个孩子,那么它会更新一个值,否则它会创建它。

in case you are wondering why the json have an array even for a single child ie because in case if we want to add data to particular child we can easily add.如果您想知道为什么 json 甚至为单个孩子也有一个数组,即因为如果我们想向特定孩子添加数据,我们可以轻松添加。

Please keep in mind this was just test code I in readFileResult() but I want to change entire structure.请记住,这只是我在 readFileResult() 中的测试代码,但我想更改整个结构。

Update:- as I know the exact path when user wants to add data I can easily add data to that path but the problem is how to check how to check if there exists data?更新:- 因为我知道用户想要添加数据时的确切路径,所以我可以轻松地将数据添加到该路径,但问题是如何检查如何检查是否存在数据? As I've nested child in Arrays('{"root":[{"abc":[{"xyz":[{"pq":"yo"}]}]}]}') I can not loop through each child and parse it coz it would be consuming a lot of resources and time.因为我在 Arrays('{"root":[{"abc":[{"xyz":[{"pq":"yo"}]}]}]}') 中嵌套了孩子,我无法循环每个孩子并解析它,因为它会消耗大量资源和时间。

Another Update:- I've changed the structure to rfc8259 to get rid of these arrays.另一个更新:-我已将结构更改为 rfc8259 以摆脱这些数组。 This should be simpler but still struggling in "updating" the data.这应该更简单,但仍然在“更新”数据方面苦苦挣扎。

I found another better way to fix this, I changed the structure a little bit.我找到了另一种更好的方法来解决这个问题,我稍微改变了结构。 and Instead of using JSONWriter and JSONArray, I just used JSONObjects to query and edit objects easily.而不是使用 JSONWriter 和 JSONArray,我只是使用 JSONObjects 来轻松地查询和编辑对象。 You can check this out.你可以看看这个。

For inserting objects to JSON if it does not exit or updating if it exists, I think there are multiple ways.对于在 JSON 不退出或更新(如果存在)的情况下将对象插入到 JSON 中,我认为有多种方法。 Now I don't know your limitations of use case but simplest one would be like below pseudo code :现在我不知道您的用例限制,但最简单的一个如下伪代码:

- get root level json object

- get input from user e.g. abc/xyz/lm=ayo

- split input first by '=' for value at index 1 and then by '/' for keys.

- Now iterate over keys
    + Find in object is key exists.
    + If key exists, get json object for that key and check for next keys inside this object.
    + If key does not exist, add keys in hierarchy and at last key, store above value. 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM