Advertisement
Guest User

JSON, might help, might not.

a guest
Aug 22nd, 2015
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "../jsmn.h"
  4.  
  5. /*
  6. * A small example of jsmn parsing when JSON structure is known and number of
  7. * tokens is predictable.
  8. */
  9.  
  10. //This is the actual JSON string, you could load this with the FS (see ctrulib)
  11. const char *JSON_STRING =
  12. "{\"user\": \"johndoe\", \"admin\": false, \"uid\": 1000,\n "
  13. "\"groups\": [\"users\", \"wheel\", \"audio\", \"video\"]}";
  14.  
  15. static int jsoneq(const char *json, jsmntok_t *tok, const char *s) {
  16. if (tok->type == JSMN_STRING && (int) strlen(s) == tok->end - tok->start &&
  17. strncmp(json + tok->start, s, tok->end - tok->start) == 0) {
  18. return 0;
  19. }
  20. return -1;
  21. }
  22.  
  23. int main() {
  24. int i;
  25. int r;
  26. jsmn_parser p;
  27. jsmntok_t t[128]; /* We expect no more than 128 tokens */
  28.  
  29. //Initialize the JSMN libary
  30. jsmn_init(&p);
  31. //Parses JSON string and fill tokens
  32. r = jsmn_parse(&p, JSON_STRING, strlen(JSON_STRING), t, sizeof(t)/sizeof(t[0]));
  33. if (r < 0) {
  34. printf("Failed to parse JSON: %d\n", r);
  35. return 1;
  36. }
  37.  
  38. /* Assume the top-level element is an object */
  39. if (r < 1 || t[0].type != JSMN_OBJECT) {
  40. printf("Object expected\n");
  41. return 1;
  42. }
  43.  
  44. /* Loop over all keys of the root object */
  45. for (i = 1; i < r; i++) {
  46. //jsoneq(FULLSTRING, pointer to t (jsmntok_t), name of "option"/json variable name)
  47. if (jsoneq(JSON_STRING, &t[i], "user") == 0) {
  48. /* We may use strndup() to fetch string value */
  49. printf("- User: %.*s\n", t[i+1].end-t[i+1].start,
  50. JSON_STRING + t[i+1].start);
  51. i++;
  52. } else if (jsoneq(JSON_STRING, &t[i], "admin") == 0) {
  53. /* We may additionally check if the value is either "true" or "false" */
  54. printf("- Admin: %.*s\n", t[i+1].end-t[i+1].start,
  55. JSON_STRING + t[i+1].start);
  56. i++;
  57. } else if (jsoneq(JSON_STRING, &t[i], "uid") == 0) {
  58. /* We may want to do strtol() here to get numeric value */
  59. printf("- UID: %.*s\n", t[i+1].end-t[i+1].start,
  60. JSON_STRING + t[i+1].start);
  61. i++;
  62. } else if (jsoneq(JSON_STRING, &t[i], "groups") == 0) {
  63. int j;
  64. printf("- Groups:\n");
  65. if (t[i+1].type != JSMN_ARRAY) {
  66. continue; /* We expect groups to be an array of strings */
  67. }
  68. for (j = 0; j < t[i+1].size; j++) {
  69. jsmntok_t *g = &t[i+j+2];
  70. printf(" * %.*s\n", g->end - g->start, JSON_STRING + g->start);
  71. }
  72. i += t[i+1].size + 1;
  73. } else {
  74. printf("Unexpected key: %.*s\n", t[i].end-t[i].start,
  75. JSON_STRING + t[i].start);
  76. }
  77. }
  78. return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement