Go Down

Topic: Serial print Json (Read 2905 times) previous topic - next topic

ecabanas

Hi all,

I'm sending info to Amazon IOT via Yun. I'm using this expression to print the message:

Code: [Select]
sprintf(msg, "Temperature %d ", t) ;

The problem is that I want to write a Json expression like this:

Code: [Select]
{ "id" :" 004", "Date":"01/01/2016","Temperature ":"30",}

and the problem is how could i serial print a quotation marks?

Many thank's guys

Eduard

ShapeShifter

Character String Escape Sequences

To include a double quote in a character string, make it an escape sequence by adding a backslash in front of the double quote. For example, this code:
Code: [Select]
Serial.print("\"Hello!\"");
Will print out:
Quote
"Hello!"
The link above shows how to include other special characters.

ecabanas

Hi ShapeShifter

Many thank's and now is a little bit more clear. The problem is I want to write this

{ "id" :" 004","Temperature ":"30",}

Being 30 a variable. I tryied to write:

Code: [Select]
sprintf(msg,"\"{Id\":\"001\" "," \"Temperature\": %d", t);

beiing t the temp variabe, but returns to me an error

Many thank's man!

ShapeShifter

When saying you have an error, it's always helpful if you post the exact error message you're getting. Without it, we're just trying to guess at the problem.

But in this case, I can try to guess: you have a comma in the middle of the string, with double quotes on either side - I don't think you want those extra quotes there, I think you want that comma to be part of the string.

As you have it coded, you've got four parameters to the sprintf() call:
  • msg
  • "\"{Id\":\"001\" "
  • " \"Temperature\": %d"
  • t


I believe your two strings should be one string, I think you're missing your closing brace, and you don't have the quotes around the value for variable t. Perhaps this is what you need:
Code: [Select]
sprintf(msg,"\"{Id\":\"001\",\"Temperature\": \"%d\"}", t);

Go Up