如何在不使用副本的情况下更改QJson层次结构中的QJsonObject值? [英] How to change QJsonObject value in a QJson hierarchy without using copies?

查看:684
本文介绍了如何在不使用副本的情况下更改QJson层次结构中的QJsonObject值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在将Qt5.0与核心QJson库一起使用,以处理我正在开发的程序的一些数据.

I am currently using Qt5.0 with the core QJson library to handle some data for the program I am developing.

要设置此问题的场景,我将向您提供一些说明我的问题的JSON数据:

To set the scene for this question I will provide you with some JSON data that illustrates my problem:

{
    "CLOCKS": [
        {
            "ID": "clk",
            "MAX": 2e+08,
            "MIN": 1e+07,
            "VALUE": "no_clock"
        },
        {
            "ID": "memclk",
            "MAX": 2e+08,
            "MIN": 1e+07,
            "VALUE": "memclk"
        }
    ]
}

在这里,我们有一个包含单个键"CLOCKS"的父QJsonObject.此键的值是QJsonObjects的QJsonArray,其中包含许多包含我的数据的键/值对.

Here we have a parent QJsonObject containing a single key 'CLOCKS'. The value for this key is a QJsonArray of QJsonObjects that contain a number of key/value pairs that contain my data.

如果我想检索ID为'clk'的QJsonObject,我目前正在使用这样的代码:

If I wanted to retrieve the QJsonObject with id 'clk' I am currently using code like this:

// imagine m_data is my parent QJsonObject
QJsonArray clocks = m_data["CLOCKS"].toArray();
foreach (const QJsonValue & value, clocks) {
    QJsonObject obj = value.toObject();
    if (obj["ID"].toString() == "clk") {
        return obj;
    }
}

这很好用,到目前为止该库很棒.但是,最近我想获取QJsonObject 引用(而不是副本)进行修改时就遇到了问题.

This works fine and the library has been great so far. However, I have started running into issues recently when I want to obtain a QJsonObject reference for modification instead of a copy.

所以我的问题是,鉴于提供了示例数据,我如何获得QJsonObject引用以修改所需时钟数据对象中的键/值对.由于您可以获取QJsonValueRefs(它们是对值条目的引用)这一事实,因此问题就表现为IMO,但是要实际访问其中的数据(如果值是另一个数组/对象),则必须使用toArray进行转换. (),toObject()函数等.此函数仅返回副本,不返回引用,这为使用引用迭代对象层次结构提供了障碍.

So my question is, given the sample data provided how do I obtain a QJsonObject reference in order to modify the key/value pairs in the desired clock data object. The problem manifests itself, IMO due to the fact that you can obtain QJsonValueRefs, which are references to the value entries... but to actually access the data inside this (if the value is another array/object) you must convert using the toArray(), toObject() functions etc. This functions only return copies and not references creating a barrier to iterating down the object hierarchy with references.

到目前为止,解决这个问题的唯一方法是创建整个"CLOCKS" QJsonArray的副本,找到我想要的对象,然后将其删除并使用更改后的数据重新插入...最后将整个数组分配回父对象中的"CLOCKS"键.对我来说,这似乎很麻烦,以至于我觉得自己做错了,必须有更好的方法.

The only way I have come up with so far to get around this is to create a copy of the entire "CLOCKS" QJsonArray, find the object I want then delete it and reinsert it with the changed data... and finally assign the entire array back to the "CLOCKS" key in the parent object. This seems cumbersome enough to me to me that I feel like I am doing something wrong and there must be a better way.

到目前为止,这里要支持我的代码,只是为了更改其中一个时钟QJsonObjects的"VALUE":

To support this here is what my code looks like so far... just to change the "VALUE" for one of the clock QJsonObjects:

  QJsonArray resets = m_data.value(TAG_RESETS).toArray();

  // get a copy of the QJsonObject
  QJsonObject obj;
  foreach (const QJsonValue & value, resets) {
    QJsonObject o = value.toObject();
    if (o.value(TAG_ID).toString() == id) {
      obj = o;
      break;
    }
  }

  // modify the object
  obj[TAG_VALUE] = "NEW VALUE";

  // erase the old instance of the object
  for (auto it = resets.begin(); it != resets.end(); ++it) {
    QJsonObject obj = (*it).toObject();
    if (obj.value(TAG_ID).toString() == id) {
      resets.erase(it);

      // assign the array copy which has the object deleted
      m_data[TAG_RESETS] = resets;
      break;
    }
  }   

  // add the modified QJsonObject
  resets.append(obj);

  // replace the original array with the array containing our modified object
  m_data[TAG_RESETS] = resets;

我知道可以将其缩短一点,但是似乎仍然需要一种更好的方法来更改QJson对象层次结构中的单个值,而无需付出所有这些努力!

I know this could be shortened a little bit but it still seems like there must be a better way to change a single value in a QJson object hierarchy without going to all this effort!!!

推荐答案

根据Qt开发人员的信息,他实际上是在Qt5中编写了QJson-

According to information from Qt developer who actually wrote QJson in Qt5 -

Qt当前包含的是一种只读"实现,以提供解析功能.他打算在将来使用参考"支持来扩展设计,但尚未完成.

What's currently included in Qt is a 'read-only' implementation to provide parsing facilities. He has an intention to extend design with 'references' support in future, but it's not yet done.

这篇关于如何在不使用副本的情况下更改QJson层次结构中的QJsonObject值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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