Gather data from a JSON File
응답
19. 6. 25 오전 8:57

Hey everyone,

I want to read data from a JSON file and store it in the corresponding variables.

I have read through some of the forum like stackoverflow, to find that you would need additional libraries.

Most of the posts suggested JSON CPP. Which i can't import by just using:

#include "jsconcpp/json/json.h"

The problem probably beeing, that i have to manually include the source file/library, but how do i do that?

 

Or is there any better way to extract values from a JSON that looks like the following:

{  
   "XDKMacAddress" : "A8-1B-6A-9D-4D-2E",
   "EnviromentEnable" : "True",
   "AccelerometerEnable" : "False",
   "GyroscopeEnable" : "False",
   "IntertialEnable" : "True",
   "LightEnable" : "True",
   "MagnetometerEnable" : "False",
   "AcousticEnable" : "False",
   "SampligRate" : "5000"
}

As you might see, i want to store those values in the corresponding variables in my sourcecode. I get this JSON file from a HTTP GET Request (like the one from the standard example). And to get those values all I've found was to convert this "const char*" to a JSON format and then with JSON CPP gather the values.

 

I'll be grateful for any help provided.

 

PS: What C++ Version does the XDK-Workbench 3.6.0 use?

0 (0 투표)
RE: Gather data from a JSON File
응답
19. 6. 26 오전 8:33 as a reply to Martin Scheck.

Hi Martin,

As far as I know there is a Json included in the Workbench. See the picture below.
You just have to include it as follows:

#include "cJSON.h"

Here are the APIs: http://amz-secrets.de/html/c_j_s_o_n_8h.html

The Workbench is designed for C. However, it is possible to use C++. See here: https://xdk.bosch-connectivity.com/community/-/message_boards/message/248231

0 (0 투표)
RE: Gather data from a JSON File
응답
19. 6. 26 오전 11:44 as a reply to Saeid Kajlar.

Hi Saeid,

thanks for the response.

I have looked through the APIs you provided. It looks like this cJSON is just a manual way to generate a JSON file.

What I want to do is to retrieve values from a JSON, or rather a String that i get from a REST Request that is formatted as a JSON file.

And out of this JSON-String (as provided above) I want to retrieve the values.

Solving this problem i've found this example where they take the String and automatically convert it to a JSON object, where you can easily get the values.

I tried to import this JSON-CPP library with the guide provided here.

This guide is made for the workbench 3.0.0. When i tried to make the library as an library object, i got a lot of errormessages. Since it's a library that is used by a lot of people, i dont quite think the problem lies in the library and more at the guide that is not up to the current workbenchversion of 3.6.0.

Is there maybe an alternative way to import libraries or to extract values from a JSON string?

 

Thanks in advance for your help

0 (0 투표)
RE: Gather data from a JSON File
응답
19. 6. 26 오후 1:29 as a reply to Martin Scheck.

Hi Martin,

How to extract values from a JSON string:

yourString:

{
  "blabla" : 1000,
  ...
}

implementation:

cJSON * jsonContext= NULL;
const cJSON *blabla = NULL;

jsonContext= cJSON_ParseWithOpts(yourString, 0, 1);
blabla = cJSON_GetObjectItem(jsonContext, "blabla");

 

Please read the API description I've already given you.

regards

Saeid

 

 

+1 (1 투표)
RE: Gather data from a JSON File
응답
19. 7. 2 오전 6:51 as a reply to Saeid Kajlar.

aaah, now I understood it. Thank you for your patience and help! This works perfectly fine!

 

0 (0 투표)