Qt修改JSON文件 [英] Qt modifying a JSON file

查看:1676
本文介绍了Qt修改JSON文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够读取现有的JSON文件,对其进行修改(例如,替换,删除和添加对象,数组和键值对),然后再次将该文件写出.

I need to be able to read in an existing JSON file, make modifications to it (such as replacing, removing and adding objects, arrays and key-value pairs), and then write the file out again.

我正在尝试读写具有以下内容的JSON文件:

I have am trying to read and write to a JSON file with these contents:

{
    "array": [
        {
            "name": "Channel",
            "default": 1

        },
        {
            "name": "Size",
            "default": 457
        }
    ]
}

我正在成功读取文件,但是无法使用以下代码对其进行任何更改:

I am reading the file in successfully, but failing to make any changes to it using the following code:

QFile File("/path/to/myfile.json");
File.open(QIODevice::ReadOnly | QIODevice::Text);

QJsonParseError JsonParseError;
QJsonDocument JsonDocument = QJsonDocument::fromJson(File.readAll(), &JsonParseError);

File.close();

QJsonObject RootObject = JsonDocument.object();
QJsonArray Array = RootObject.value("array").toArray();

QJsonObject ElementOneObject = Array.at(0).toObject();

ElementOneObject.insert("key", QJsonValue(QString("value")));
ElementOneObject.insert("name", QJsonValue(QString("David")));

File.open(QFile::WriteOnly | QFile::Text | QFile::Truncate);
File.write(JsonDocument.toJson());
File.close();

我希望看到数组的第一个元素具有新名称"David"和新的键值对,例如"key" : "value".运行此代码后,文件的内容相同.我知道文件已被写出,因为键值对的排序已更改为按按字母顺序排序的键排序.

I am expecting to see the first element of the array to have a new name of "David" and a new key-value pair like "key" : "value". The contents of the file are identical after this code has run. I know the file has been written out, because the ordering of the key-value pairs has been changed to be ordered by the keys sorted into alphabetic order.

如何获取文件以反映我尝试进行的更改?

How do I get the file to refelct the changes I have tried to make?

推荐答案

问题中的示例不起作用的原因是因为JsonDocument.object()RootObject.value("array").toArray()Array.at(0).toObject()都返回数据的副本,而不是引用.有两种不同的处理方式.

The reason the example in the question does not work is because JsonDocument.object(), RootObject.value("array").toArray() and Array.at(0).toObject() all return copies of the data, not references. There are two different ways to handle this.

1)对元素进行更改后,将其插入数组的副本中,然后将数组插入RootObject副本中,然后在JsonDocument上设置RootObject

1) After making you changes to the element, insert it into the copy of your array, then insert the array into the RootObject copy, then set the RootObject on the JsonDocument

Array.removeAt(0);
Array.insert(0, ElementOneObject);
RootObject.insert("array", Array);
JsonDocument.setObject(RootObject);

2)使用find()获取要修改的对象/值的引用

2) Use find() to get references to the objects/values you want to modify

QJsonObject RootObject = JsonDocument.object();
QJsonValueRef ArrayRef = RootObject.find("array").value();
QJsonArray Array = ArrayRef.toArray();

QJsonArray::iterator ArrayIterator = Array.begin();
QJsonValueRef ElementOneValueRef = ArrayIterator[0];

QJsonObject ElementOneObject = ElementOneValueRef.toObject();

// Make modifications to ElementOneObject

ElementOneValueRef = ElementOneObject;
ArrayRef = Array;
JsonDocument.setObject(RootObject);

这篇关于Qt修改JSON文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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