I have the following string in the form of json:
{
"num":1,
"data":{
"city":"delhi"
}
}
I need to get the value of "num" key using sscanf. Here is my attempt. I know it's incorrect. But I don't know how to do it.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
char *str = "{\"num\":1,\"data\":{\"city\":\"delhi\"}}";
char *ret = malloc(sizeof(char) * 10);
sscanf(str, "{\"num\":%s, %s" , ret);
printf("%s", ret);
return 0;
}
Any suggestions?
int num = atoi(strchr(str, ':')+1);or use a json parser, if the string can vary.