Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decode json in c++

Tags:

c++

json

I have saved my datas in json form in MySQL database. I want to decode it in c++. I used SimpleJSON to encode it but I am not able to extract it. My database format is :

 [[{"x":36},{"y":0},{"value":0.47873455286026}],
[{"x":68},{"y":0},{"value":0.00944233685731888}],
[{"x":35},{"y":0},{"value":0.00944233685731888}],
[{"x":206},{"y":0},{"value":0.00944233685731888}]]

saved in 1 column. I get this value as a string for the database. How can I decode it? The code I used to encode is:

  JSONArray array;
     for (int j = 0;j<bb.rows;j++ ){
         JSONArray v;
        JSONObject x,y,value,result;

        x[L"x"]=new JSONValue ((double) (bb.at<double>(j,0)));
        y[L"y"]=new JSONValue ((double)(0));
        value[L"value"]=new JSONValue(bb.at<double>(j,1));

         v.push_back(new JSONValue(x));
        v.push_back(new JSONValue(y));
        v.push_back(new JSONValue(value));
        array.push_back(new JSONValue(v));
    }

I followed this link. https://github.com/MJPA/SimpleJSON

like image 674
user1583647 Avatar asked Oct 24 '25 10:10

user1583647


1 Answers

#include "JSON.h"
#include "iostream"

int main() {

    char *json_string = "[[{\"x\":36},{\"y\":0},{\"value\":0.47873455286026}], \
        [{\"x\":68},{\"y\":0},{\"value\":0.00944233685731888}], \
        [{\"x\":35},{\"y\":0},{\"value\":0.00944233685731888}], \
        [{\"x\":206},{\"y\":0},{\"value\":0.00944233685731888}]]";

    JSONValue *data = JSON::Parse(json_string);

    for( size_t i=0; i<data->CountChildren(); ++i ) {
        JSONValue *row = data->Child(i); 
        std::cout << "x = " << row->Child(0)->Child(L"x")->AsNumber() << std::endl;
        std::cout << "y = " << row->Child(1)->Child(L"y")->AsNumber() << std::endl;
        std::cout << "value = " << row->Child(2)->Child(L"value")->AsNumber() << std::endl;
        std::cout << std::endl;
    }

    return 0;
}

Here is this code to test it online - http://ideone.com/wBRZfI (see the code in the end, because whole SimpleJSON source was pasted there)

like image 98
mas.morozov Avatar answered Oct 27 '25 00:10

mas.morozov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!