I have to admit that I can't really use this function.
I'm trying to format some json but it crashing all the time my arduino:
char response[300];
tmp = "123";
snprintf(response, 300, "[{Name:'Furnace',Value=%s,Alarm={MinValue=%s,MaxValue=%s}},{Name:'Room',Value=%s,Alarm={MinValue=%s,MaxValue=%s}}]", tmp);
How should I use this function properly?
-
That's not JSON.Ignacio Vazquez-Abrams– Ignacio Vazquez-Abrams2015年03月08日 21:52:06 +00:00Commented Mar 8, 2015 at 21:52
1 Answer 1
char response[300];
tmp = "123";
snprintf(response, 300,
"[{Name:'Furnace',Value=%s,Alarm={MinValue=%s,MaxValue=%s}},"
"{Name:'Room',Value=%s,Alarm={MinValue=%s,MaxValue=%s}}]",
tmp, tmp, tmp, tmp, tmp, tmp);
In other words, you should add, after the format string, as many extra parameters as there are %s in the format string.
PS: This looks like JSON, but it is not really valid JSON. The format below should give valid JSON:
"[{\"Name\":\"Furnace\",\"Value\":%s,"
"\"Alarm\":{\"MinValue\":%s,\"MaxValue\":%s}},"
"{\"Name\":\"Room\",\"Value\":%s,"
"\"Alarm\":{\"MinValue\":%s,\"MaxValue\":%s}}]",
answered Mar 8, 2015 at 21:54
lang-cpp