本文介绍了更新TJSONArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何正确更新json值?这是我尝试过的,但是没有运气:

How to correctly update json value?Here is what I tried, but without luck:

TJSONArray *Data;
Data = (TJSONArray*) TJSONObject::ParseJSONValue(TEncoding::ASCII->GetBytes(json),0);
TJSONObject *obj = (TJSONObject*) Data->Get(0);
TJSONArray *arr = (TJSONArray*) obj->Get("tokens");
arr = (TJSONArray*) TJSONObject::ParseJSONValue(TEncoding::ASCII->GetBytes(arrjson),0);

数据:

[{"source":"aaaa","cluster":"1","tokens":[{},{}]}, {"source":"bbbb","cluster":"2","tokens":[{},{}]}] 

推荐答案

不幸的是, TJSONArray 不能本地允许您用新值替换现有元素(为什么?谁?知道).您所能做的就是添加和删除元素,以及枚举元素.

Unfortunately, TJSONArray does not natively allow you to REPLACE existing elements with new values (Why? Who knows). All you can do is ADD and REMOVE elements, and ENUMERATE elements.

由于您的数组包含 TJSONObject 实例,因此要将此类元素替换为新对象,您将必须执行以下操作之一:

Since your array holds TJSONObject instances, to replace such an element with a new object, you will have to either:

  1. 从数组中删除所有元素,包括并跟随所需的元素索引,然后添加所需类型的新对象,然后重新添加已删除的元素(按其先前的顺序).

  1. remove all of the elements from the array including and following the desired element index, then add a new object of the desired type, and then re-add the removed elements (in their previous order).

构造一个全新的 TJSONArray ,然后根据需要向其添加对象,并在需要时从以前的 TJSONArray 中复制值.然后,您可以为 tokens 字段获取 TJSONPair ,并将新的 TJSONArray 设置为其 JsonValue .

construct a completely new TJSONArray, and then add objects to it as needed, copying values from the previous TJSONArray where needed. You can then get the TJSONPair for the tokens field and set the new TJSONArray as its JsonValue.

如果您不喜欢这种方法,请寻找一个支持您感兴趣的编辑功能的第三方JSON库.

If this is not to your liking, then look for a 3rd party JSON library that supports the editing capabilities you are interested in.

这篇关于更新TJSONArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-19 04:46