简体   繁体   中英

How to convert C++ string array to json?

I have started implementing Microsoft Cognitive Services using C++. I have a C++ String array(faceIds array)

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.

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?

Thank in Advance.

Your question doesn't precisely identify your input and expected output. Are you parsing the C++ from a file? 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.

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.

If you simply want to iterate in C++ through faceIds, then the following code should produce the desired JSON string:

#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. 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. One can't uniquely identify the library from an object name like JSONObject, which is a class name used in dozens of libraries.

This is a robust cross platform solution to working with JSON in C++ https://github.com/nlohmann/json . I'm sure Microsoft has some library locked to their own OS too. The examples are clear.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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