简体   繁体   English

如何将 C++ 字符串数组转换为 json?

[英]How to convert C++ string array to json?

I have started implementing Microsoft Cognitive Services using C++.我已经开始使用 C++ 实现Microsoft 认知服务 I have a C++ String array(faceIds array)我有一个 C++ 字符串数组(faceIds 数组)

string faceIds[] ={
            "29e874a8-a08f-491f-84e8-eac263d51fe1",
            "6f89f38a-2411-4f6c-91b5-15eb72c17c22",
            "7284b730-6dd7-47a3-aed3-5dadaef75d76",
            "1fc794fa-3fd4-4a78-af11-8f36c4cbf14c",
            "3e57afca-bd1d-402e-9f96-2cae8dbdfbfa",
            "c2a4e0f5-4277-4f5a-ae28-501085b05209",
            "23b5910e-9c32-46dd-95f3-bc0434dff641"
    };

Then, I try to convert string array(C++) to json string.然后,我尝试将字符串数组(C++)转换为json字符串。

JSONObject jsnobject = new JSONObject(10);
JSONArray jsonArray = jsnobject.getJSONArray(faceIds);
    for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject explrObject = jsonArray.getJSONObject(i);
}

But, I got problem.但是,我遇到了问题。 So, My question is, How to convert C++ string array to json?所以,我的问题是,如何将 C++ 字符串数组转换为 json?

Thank in Advance.预先感谢。

Your question doesn't precisely identify your input and expected output.您的问题并没有准确识别您的输入和预期输出。 Are you parsing the C++ from a file?你是从文件中解析 C++ 吗? I can't tell.我说不清。

If the first code block is an autogenerated input file and will always have that whitespace pattern and the JSON equivalent is your desired output, replace the first line with "[\\n" and the last line with "]/n" and you're done.如果第一个代码块是自动生成的输入文件,并且始终具有该空白模式,并且等效的 JSON 是您想要的输出,请将第一行替换为“[\\n”,最后一行替换为“]/n”完毕。

If you can't guarantee the white space pattern of the input file, then you will need a C++ parser to generate an AST (abstract symbol tree) that you can traverse to find the faceIds array RHS (right hand side) and then do the same thing as shown below from that AST collection.如果您不能保证输入文件的空白模式,那么您将需要一个 C++ 解析器来生成一个 AST(抽象符号树),您可以遍历它以找到 faceIds 数组 RHS(右侧),然后执行与该 AST 集合中显示的内容相同。

If you simply want to iterate in C++ through faceIds, then the following code should produce the desired JSON string:如果您只是想通过 faceIds 在 C++ 中进行迭代,那么以下代码应生成所需的 JSON 字符串:

#include <iostream>
#include <sstream>

std::string faceIds[] = {
    "29e874a8-a08f-491f-84e8-eac263d51fe1",
    "6f89f38a-2411-4f6c-91b5-15eb72c17c22",
    "7284b730-6dd7-47a3-aed3-5dadaef75d76",
    "1fc794fa-3fd4-4a78-af11-8f36c4cbf14c",
    "3e57afca-bd1d-402e-9f96-2cae8dbdfbfa",
    "c2a4e0f5-4277-4f5a-ae28-501085b05209",
    "23b5910e-9c32-46dd-95f3-bc0434dff641"
};

int main() {

    std::ostringstream ostr;
    ostr << '[' << std::endl;
    int last = std::extent<decltype(faceIds)>::value - 1;
    int i = 0;
    while (i < last)
        ostr << "    \"" << faceIds[i ++] << "\"," << std::endl;
    ostr << "    \"" << faceIds[i] << "\"" << std::endl;
    ostr << ']' << std::endl;

    std::cout << ostr.str();

    return 0;
}

If you want some library's object representation, then you'll have to identify what library you are using so we can review its API.如果您想要某个库的对象表示,那么您必须确定您使用的是哪个库,以便我们可以查看其 API。 Whatever library you use, you could always just run whatever parse method it has on ostr.str() above, but we could find a more efficient method to build the equivalent JSON tree if you identified the JSON library.无论你使用什么库,你总是可以运行上面 ostr.str() 上的任何解析方法,但是如果你确定了 JSON 库,我们可以找到一种更有效的方法来构建等效的 JSON 树。 One can't uniquely identify the library from an object name like JSONObject, which is a class name used in dozens of libraries.不能从像 JSONObject 这样的对象名称中唯一地标识库,JSONObject 是数十个库中使用的类名。

This is a robust cross platform solution to working with JSON in C++ https://github.com/nlohmann/json .这是一个强大的跨平台解决方案,用于在 C++ https://github.com/nlohmann/json 中使用 JSON。 I'm sure Microsoft has some library locked to their own OS too.我确信微软也有一些库锁定到他们自己的操作系统上。 The examples are clear.例子很清楚。

我认为nlohmann c++ 库在您的情况下很有用。

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

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