João Amaro
João Amaro

Reputation: 496

How to convert a string to json format in c++?

So I'm reading some values from a sensor via SPI. I already converted those values to a string (don't know if I should but I was trying something). Now I can't convert them to json format. Here is my code:

#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;
}

This is the result:

1023 1023 1023 1023 1023

I want the result to be this way:

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

Can you guys help me with this?

Upvotes: 1

Views: 4904

Answers (2)

Richard Hodges
Richard Hodges

Reputation: 69854

this code:

#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;
}

results in:

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

update:

this version will behave correctly with a c++11 compiler (and highlights some 'new' features of c++14 - but let's not live in the stone age too long eh?)

#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;
}

Upvotes: 1

eldruin
eldruin

Reputation: 342

As you appear to be using C++ and the STL in an embedded system, I would recommend you using picojson. It is just a 1-header library and would be better than implementing the serialization yourself with some string manipulation. If you do so, it will get uglier later on if you extend your json output.

Upvotes: 0

Related Questions