簡體   English   中英

如何在C ++中將字符串轉換為JSON格式?

[英]How to convert a string to json format in c++?

因此,我正在通過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的“新”功能-但讓我們不要活在石器時代嗎?)

#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 ++和STL時,我建議您使用picojson 它只是一個1頭庫,比通過一些字符串操作自己實現序列化要好。 如果這樣做,則如果擴展json輸出,稍后將變得更難看。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM