I am trying to use sscanf to extract specific values of name and msg from a string {"username":"ece","says":"hello"}
as following:
sscanf(data, "{"\username"\:"\%s"\,"\says"\:"\%s"\}", name, msg);
I need 'ece' in name and 'hello' in msg but I am getting ece","says":"hello" in name and msg remains empty.
The %s
format stops at the next white space. You need it to stop earlier, at the '"'
, so you need to use a character set,
sscanf(data, "{\"username\":\"%[^\"]\",\"says\":\"%s\"}", name, msg);
^^^^^^
read up to the next double quote
You need to put the escape \
before the escaped character.
sscanf(data, "{\"username\":\"%s\",\"says\":\"%s\"}", name, msg);
And unless there is a white space after the username, all that's in the buffer will be read into name
.
Use an inverse character set instead of %s, like this %[^\"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With