Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert Json to uint8_t #1008

Closed
qvs5010 opened this issue Mar 12, 2018 · 8 comments
Closed

Convert Json to uint8_t #1008

qvs5010 opened this issue Mar 12, 2018 · 8 comments

Comments

@qvs5010
Copy link

qvs5010 commented Mar 12, 2018

Hi
How can I convert my JSON to a uint8_t?
is this even possible?

uint8_t data[64] = theJsonData;

@nlohmann
Copy link
Owner

What should be the content? The byte representation of the JSON text?

@nlohmann nlohmann added the state: needs more info the author of the issue needs to provide more details label Mar 12, 2018
@qvs5010
Copy link
Author

qvs5010 commented Mar 12, 2018

Hi

My json content looks like this {"lat": -24.673566818237305,"lon": 25.937171936035156}

Code:

static json theJsonData;

float float_lat = 0.0f;
float float_lon = 0.0f;
ifstream inf("/home/pi/ttn-ulm-node-dragino-master/data/geo.dat");
if (inf) {
         for (int i = 0; i < 2; i++) {
             std::string coord;
             getline(inf, coord);
           if (i == 0) {
             float_lat = stof(coord);
           }
           if (i == 1) {
             float_lon = stof(coord);
           }
        }
}else{
          printf("Failed!");
}

theJsonData["lat"] = float_lat;
theJsonData["lon"] = float_lon;

uint8_t data[64] = theJsonData; <-- geting error on compiling.

LMIC_setTxData2(1, data, sizeof(data)-1, 0);

@theodelrieu
Copy link
Contributor

Functions cannot return C arrays, so you won't be able to do that. You could instead use std::array.

@nlohmann
Copy link
Owner

So theJsonData.dump(); would return a JSON text representation as std::string. From there, you can copy it into an std::array or uint8_t[] manually - assuming this is your usecase.

@nlohmann
Copy link
Owner

@qvs5010 Do you need further assistance?

@qvs5010
Copy link
Author

qvs5010 commented Mar 13, 2018

Hi
thanks
I did the following to get it right.

char str_W1[20];
char str_W2[20];

sprintf((char *) str_W1, "%.6f", float_lat);
sprintf((char *) str_W2, "%.6f", float_lon);

strcpy((char *) mydata,"{\"Lat\":");
strcat ((char *) mydata, str_W1);
strcat ((char *) mydata, ",\"Lon\":");
strcat ((char *) mydata, str_W2);
strcat ((char *) mydata, "}");

LMIC_setTxData2(1, mydata, strlen((char *)mydata), 0);

@nlohmann nlohmann removed the state: needs more info the author of the issue needs to provide more details label Mar 13, 2018
@nlohmann
Copy link
Owner

Thanks for checking back!

@ashish-iottive
Copy link

Thanks for checking back!

I want to convert a 110B Json file into unit8_t within limit upto 20 bytes .
I am working on BLE Notification in ESP32 Arduino

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants