使用sscanf从c中的字符串中提取

WiD*_*ata 4 c string scanf character

我正在尝试使用sscanf从字符串中提取名称和msg的特定值,{"username":"ece","says":"hello"}如下所示:

sscanf(data, "{"\username"\:"\%s"\,"\says"\:"\%s"\}", name, msg);
Run Code Online (Sandbox Code Playgroud)

我需要在名称中使用'ece'并在味精中使用'hello',但是我要获得ece“,” says“:” hello“的名称,而味精仍为空。

Dan*_*her 5

该%s格式停止在下一个空白。您需要将其停在前面,'"'因此您需要使用字符集,

sscanf(data, "{\"username\":\"%[^\"]\",\"says\":\"%s\"}", name, msg);
                              ^^^^^^
                          read up to the next double quote
Run Code Online (Sandbox Code Playgroud)