RyanJsonBaseTestForEachJson.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #include "RyanJsonBaseTest.h"
  2. RyanJsonBool_e RyanJsonBaseTestForEachJson()
  3. {
  4. char *str = NULL;
  5. char jsonstr[] = "{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,\"boolFalse\":false,\"null\":null,\"item\":"
  6. "{\"inter\":16,\"double\":16."
  7. "89,\"string\":\"hello\","
  8. "\"boolTrue\":true,\"boolFalse\":false,\"null\":null},\"arrayInt\":[16,16,16,16,16],\"arrayDouble\":[16.89,16.89,"
  9. "16.89,16.89,16.89],"
  10. "\"arrayString\":[\"hello\",\"hello\","
  11. "\"hello\",\"hello\",\"hello\"],\"array\":[16,16.89,\"hello\",true,false,null],\"arrayItem\":[{\"inter\":16,"
  12. "\"double\":16.89,\"string\":"
  13. "\"hello\",\"boolTrue\":true,"
  14. "\"boolFalse\":false,\"null\":null},{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,"
  15. "\"boolFalse\":false,\"null\":null}]}";
  16. RyanJson_t json = RyanJsonParse(jsonstr);
  17. RyanJson_t item = NULL;
  18. RyanJsonArrayForEach(RyanJsonGetObjectToKey(json, "arrayDouble"), item)
  19. {
  20. if (!RyanJsonIsDouble(item) || !compare_double(16.89, RyanJsonGetDoubleValue(item))) { goto err; }
  21. }
  22. RyanJsonArrayForEach(RyanJsonGetObjectToKey(json, "arrayInt"), item)
  23. {
  24. if (!RyanJsonIsInt(item) || 16 != RyanJsonGetIntValue(item)) { goto err; }
  25. }
  26. int32_t strLen;
  27. RyanJsonObjectForEach(RyanJsonGetObjectToKey(json, "item"), item)
  28. {
  29. str = RyanJsonPrint(item, 50, RyanJsonTrue, &strLen);
  30. printf("item { %s : %s } %d\r\n", RyanJsonGetKey(item), str, strLen);
  31. RyanJsonFree(str);
  32. }
  33. RyanJsonDelete(json);
  34. return RyanJsonTrue;
  35. err:
  36. RyanJsonDelete(json);
  37. return RyanJsonFalse;
  38. }