Go Down

Topic: Arduino JSON and struct datatypes (Read 168 times) previous topic - next topic

ard24

Hi all,
I'm playing arround with the ArduinoJson.h and getting into troubles with struct datatypes. I wrote a little program:

Code: [Select]

#include <ArduinoJson.h>

struct data
{
int counter;
int number;
char text;
};

typedef data dataset;

#include <ArduinoJson.h>

void JSON()
{

StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root["number"] = dataset.number;
}

void setup()
{
dataset.number = 10;


}

void loop()
{

JSON()

}



I would like to pass a variable of the type data named dataset to the JSON function, but it shows me this error message:

Code: [Select]

JSON.ino: In function void JSON()
 
JSON.ino: 34:26: error: expected primary-expression before '.' token
   root["number"] = dataset.number
Error compiling project sources
Build failed for project 'JSON'
 
JSON.ino: In function void setup()
 
JSON.ino: 40:9: error: expected unqualified-id before '.' token
   dataset.number = 10
 
JSON.ino: In function void loop()
 
JSON.ino: 51:1: error: expected ';' before '}' token


Can anybody give me a hint to fix it?

Thank you very much!

BR
Chris

PaulS

Quote
Can anybody give me a hint to fix it?
Fire up a text editor, and fix the problem(s).

Without seeing your code, you can't expect us to help you.
The art of getting good answers lies in asking good questions.

arduarn

Code: [Select]

struct data
{
 int counter;
 int number;
 char text;
};
typedef data dataset;
...
 dataset.number = 10;

You have created a type called dataset, but not an instance of that type - a concrete variable or object.
Just remove the typedef keyword, and don't forget the semicolon after JSON().

PaulS

Quote
You have created a type called dataset,
But not a valid type.

The type that OP seems to trying to alias is struct data, not data.
The art of getting good answers lies in asking good questions.

ard24

Hi all,
thank you very much for your help. I understand now. My mistake was, I created just a new name for the datatype "data" but not an instance due to using typedef.

But now I would like to create an array of the type data called dataset[10] for example.

Code: [Select]

#include <ArduinoJson.h>

struct data
{
int counter;
int number;
char text;
};

data dataset[10];

#include <ArduinoJson.h>

void JSON()
{

StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root["number"] = dataset.number[0];
}

void setup()
{
dataset.number = 10;
}

void loop()
{
JSON();
}


But this now generates a error message non-class type:
Code: [Select]
Compiling 'test' for 'ATmega1284'
 
test.ino: In function void JSON()
 
test.ino: 19:27: error: request for member 'number' in 'dataset', which is of non-class type 'data [10]
   root["number"] = dataset.number[0]
Error compiling project sources
Build failed for project 'test'
 
test.ino: In function void setup()
 
test.ino: 24:10: error: request for member 'number' in 'dataset', which is of non-class type 'data [10]
   dataset.number = 10


Is there a trick to solve this?

Thank you very much!
Chris

PaulS

Your type is NOT data. It is struct data.

Code: [Select]
struct data dataset[10];
The art of getting good answers lies in asking good questions.

ard24

Hi PaulS,
thank you very much for your fast reply. It's a bit complicated for me, but I guess i've got it so far.

There is a difference between a datatype and a struct. I mixed it. But also by writing:
Code: [Select]
struct data dataset[10];

the compiler gives me the same error message like above?

PaulS

Quote
the compiler gives me the same error message like above?
Is that a question or a statement?

The array of objects does not have a member named number. ONE element of the array would.
The art of getting good answers lies in asking good questions.

Go Up