在JSON文件中写入 - 数据未按正确顺序插入

发布于 2025-02-10 23:33:30 字数 1314 浏览 1 评论 0原文

我正在使用QT C ++创建一个桌面应用程序,该应用程序使用文本文件并将其转换为JSON文件,例如

{
    "102": {
        "NEUTRAL": {
            "blend": "100"
        },
        "AE": {
            "blend": "100"
        }
    },
    "105": {
        "AE": {
            "blend": "100"
        },
        "NEUTRAL": {
            "blend": "100"
        }
    }
}

我正在使用的代码:

for (int i = 0; i < output_list1.size(); i++) {
        
        if (output_list1[i] == "-") {
            c_frame++;
            continue;
        }
        
        if (output_list1[i] != "NEUTRAL") {
            
            QJsonObject neutralBlendObject;
            neutralBlendObject.insert("blend", "100");
            QJsonObject phonemeObject;
            
            phonemeObject.insert("NEUTRAL", neutralBlendObject);
            QJsonObject keyBlendObject;
            keyBlendObject.insert("blend", output_list1[i].split(' ')[1]);
            

            phonemeObject.insert(output_list1[i].split(' ')[0], keyBlendObject);

            mainObject.insert(QString::number(c_frame), phonemeObject);
        }
        c_frame++;
    }

    jsonDoc.setObject(mainObject);
    file.write(jsonDoc.toJson());
    file.close();

如您所见,我首先插入中性对象,但我正在获取数据而不是数据按照正确的顺序,中性是下一个对象的第一个以下内容,而不是。

我该如何纠正此问题?

I am creating a desktop app using QT C++ that take a text file and convert it to JSON File like this example:

{
    "102": {
        "NEUTRAL": {
            "blend": "100"
        },
        "AE": {
            "blend": "100"
        }
    },
    "105": {
        "AE": {
            "blend": "100"
        },
        "NEUTRAL": {
            "blend": "100"
        }
    }
}

This is the code I am using:

for (int i = 0; i < output_list1.size(); i++) {
        
        if (output_list1[i] == "-") {
            c_frame++;
            continue;
        }
        
        if (output_list1[i] != "NEUTRAL") {
            
            QJsonObject neutralBlendObject;
            neutralBlendObject.insert("blend", "100");
            QJsonObject phonemeObject;
            
            phonemeObject.insert("NEUTRAL", neutralBlendObject);
            QJsonObject keyBlendObject;
            keyBlendObject.insert("blend", output_list1[i].split(' ')[1]);
            

            phonemeObject.insert(output_list1[i].split(' ')[0], keyBlendObject);

            mainObject.insert(QString::number(c_frame), phonemeObject);
        }
        c_frame++;
    }

    jsonDoc.setObject(mainObject);
    file.write(jsonDoc.toJson());
    file.close();

As you can see, I am inserting the NEUTRAL object first but I am getting data not in the correct order, somtimes NEUTRAL is the the first following with the next object and somtimes not.

How can I correct this issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

み格子的夏天 2025-02-17 23:33:30

在JSON中,您可以使用两个结构来保存数据:

1。)JSON对象:键/值对的集合。该集合未订购。在各种语言中,它通过词典,哈希表等通过关联数据结构实现的。内部qt通过qhash&lt; qString,qvariant&gt;容器。

2.)JSON数组:值列表的值列表(可以是JSON对象)。在编程语言中,这被认为是数组/向量/其他。 QT在内部使用QVariantList(Qlist&lt; Qvariant)。

In JSON there are two structures you can use for saving data:

1.) JSON Objects: A collection of key/value pairs. This collection is NOT ordered. In various languages its realized via associative data structures like dictionaries, hash tables etc. Internally qt saves JSON objects via QHash<QString, QVariant> containers.

2.) JSON Arrays: An ordered list of values (which can be JSON Objects). In programming languages this is realized as an array/vector/whatever. Qt uses a QVariantList (QList<QVariant) internally.

梦萦几度 2025-02-17 23:33:30

确实使用Rapidjson库而不是基于

此示例来解决它

#include <rapidjson/document.h>
#include <rapidjson/writer.h>
#include <rapidjson/stringbuffer.h>
#include "rapidjson/filewritestream.h"
#include <string>
#include "RapidJson/prettywriter.h"
using namespace rapidjson;
using namespace std;


FILE* fp = fopen("output.json", "wb"); // non-Windows use "w"

    char writeBuffer[65536];
    FileWriteStream os(fp, writeBuffer, sizeof(writeBuffer));
    Document d;
    d.SetObject();

    rapidjson::Document::AllocatorType& allocator = d.GetAllocator();

    size_t sz = allocator.Size();

    d.AddMember("version", 1, allocator);
    d.AddMember("testId", 2, allocator);
    d.AddMember("group", 3, allocator);
    d.AddMember("order", 4, allocator);

    Value tests(kArrayType);
    Value obj(kObjectType);
    Value val(kObjectType);


    string description = "a description";

    //const char *description = "a description";
    //val.SetString()
    val.SetString(description.c_str(), static_cast<SizeType>(description.length()), allocator);
    obj.AddMember("description", val, allocator);

    string help = "some help";
    val.SetString(help.c_str(), static_cast<SizeType>(help.length()), allocator);
    obj.AddMember("help", val, allocator);

    string workgroup = "a workgroup";
    val.SetString(workgroup.c_str(), static_cast<SizeType>(workgroup.length()), allocator);
    obj.AddMember("workgroup", val, allocator);

    val.SetBool(true);
    obj.AddMember("online", val, allocator);

    tests.PushBack(obj, allocator);
    d.AddMember("tests", tests, allocator);

    // Convert JSON document to string
    PrettyWriter<FileWriteStream> writer(os);
    char indent = ' '; // single space x 4
    writer.SetIndent(indent, 4);
    d.Accept(writer);

I did resolve it using rapidjson library instead of QJsonObject

Based on this example

#include <rapidjson/document.h>
#include <rapidjson/writer.h>
#include <rapidjson/stringbuffer.h>
#include "rapidjson/filewritestream.h"
#include <string>
#include "RapidJson/prettywriter.h"
using namespace rapidjson;
using namespace std;


FILE* fp = fopen("output.json", "wb"); // non-Windows use "w"

    char writeBuffer[65536];
    FileWriteStream os(fp, writeBuffer, sizeof(writeBuffer));
    Document d;
    d.SetObject();

    rapidjson::Document::AllocatorType& allocator = d.GetAllocator();

    size_t sz = allocator.Size();

    d.AddMember("version", 1, allocator);
    d.AddMember("testId", 2, allocator);
    d.AddMember("group", 3, allocator);
    d.AddMember("order", 4, allocator);

    Value tests(kArrayType);
    Value obj(kObjectType);
    Value val(kObjectType);


    string description = "a description";

    //const char *description = "a description";
    //val.SetString()
    val.SetString(description.c_str(), static_cast<SizeType>(description.length()), allocator);
    obj.AddMember("description", val, allocator);

    string help = "some help";
    val.SetString(help.c_str(), static_cast<SizeType>(help.length()), allocator);
    obj.AddMember("help", val, allocator);

    string workgroup = "a workgroup";
    val.SetString(workgroup.c_str(), static_cast<SizeType>(workgroup.length()), allocator);
    obj.AddMember("workgroup", val, allocator);

    val.SetBool(true);
    obj.AddMember("online", val, allocator);

    tests.PushBack(obj, allocator);
    d.AddMember("tests", tests, allocator);

    // Convert JSON document to string
    PrettyWriter<FileWriteStream> writer(os);
    char indent = ' '; // single space x 4
    writer.SetIndent(indent, 4);
    d.Accept(writer);

Thank you all for your time

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文