2

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?

Nick Gammon
38.9k13 gold badges69 silver badges125 bronze badges
asked Mar 8, 2015 at 21:43
1
  • That's not JSON. Commented Mar 8, 2015 at 21:52

1 Answer 1

2
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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.