c++ - 如何在C++中将字符串转换为json格式?

标签 c++ json string

所以我正在通过 SPI 从传感器读取一些值。我已经将这些值转换为字符串(不知道是否应该,但我正在尝试一些东西)。现在我无法将它们转换为 json 格式。 这是我的代码:

#include "ad7490Spi.h"
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

using namespace std;

string IntToString (int a2dVal) 
{
    ostringstream oss;  
    oss << a2dVal;  
    return oss.str();
}

int main(void)
{
    ad7490Spi a2d("/dev/spidev0.0", SPI_MODE_0, 1000000, 16);
    int i = 5;
    int a2dVal = 0; 
    int a2dChannel = 0;
    unsigned char data[3];      

    while(i > 0)
    {
        data[0] = 1;  //  first byte transmitted -> start bit
        data[1] = 0b1000000000000000 |( ((a2dChannel & 15) << 4)); // second byte transmitted -> (SGL/DIF = 1, D2=D1=D0=0)
        data[2] = 0; // third byte transmitted....don't care

        a2d.spiWriteRead(data, sizeof(data) );

        a2dVal = 0;
                a2dVal = (data[1]<< 8) & 0b1100000000; //merge data[1] & data[2] to get result
                a2dVal |=  (data[2] & 0xff);
        sleep(1);

        i--;        

    string result = IntToString (a2dVal);
    cout << " " + result + " ";

    }

return 0;
}

这是结果:

1023 1023 1023 1023 1023

我希望结果是这样的:

{
  "values": [ "1023", "1023", "1023", "1023", "1023" ]
}

你们能帮我解决这个问题吗?

最佳答案

这段代码:

#include <iostream>
#include <string>
#include <iomanip>
#include <vector>

void print_values(std::ostream& os, const std::vector<int>& v)
{
    using namespace std;

    os << "{\n";
    os << "\t\"values\" : [";
    auto sep = " ";
    for (const auto& i : v) {
        os << sep << quoted(to_string(i));
        sep = ", ";
    }
    os << " ]\n";
    os << "}\n";
}

auto main() -> int
{
    print_values(std::cout, {1,2,3,4,5,6});

    return 0;
}

结果:

{
    "values" : [ "1", "2", "3", "4", "5", "6" ]
}

更新:

此版本将在 c++11 编译器上正常运行(并突出显示 c++14 的一些"new"功能 - 但我们不要在石器时代生活太久,嗯?)

#include <iostream>
#include <string>
#include <iomanip>
#include <vector>

#if __cplusplus >= 201402L
#else
std::string quoted(const std::string& s) {
    using namespace std;
    return string(1, '"') + s + '"';
}
#endif

void print_values(std::ostream& os, const std::vector<int>& v)
{
    using namespace std;

    os << "{\n";
    os << "\t\"values\" : [";
    auto sep = " ";
    for (const auto& i : v) {
        os << sep << quoted(to_string(i));
        sep = ", ";
    }
    os << " ]\n";
    os << "}\n";
}


auto main() -> int
{
    using namespace std;

    print_values(cout,
#if __cplusplus >= 201402L
                 {1,2,3,4,5,6}
#else
                 []() -> vector<int> {
                     vector<int> v;
                     for (int i = 1 ; i < 7 ; ++i )
                         v.push_back(i);
                     return v;
                 }()
#endif
                 );

    return 0;
}

关于c++ - 如何在C++中将字符串转换为json格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34185396/

相关文章:

java - Jackson 的 JSON : Unrecognized field, 未标记为可忽略

java - 接收 JSON 并将其转换为对象的最佳实践

php - 使以 * 开头的单词加粗

c++ - 如何为带有接受模板参数的成员函数模板的类编写 C++ 概念?

c++ - bitset 数据是否以相反的顺序存储?

c++ - Callgrind:分析我的代码的特定部分

javascript - 在 JavaScript 中搜索带点的字符串

c++ - 使用 this-> 访问成员是否有任何开销?

java - json对象与java对象不一致时的gson.fromJson

string - 使用分隔符打印结构体中的字段列表