![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Sample program using picojson library. Note: it seems that there are some memory? issues with \"complex\" structures. So always test your json with the lib before using it within your application.
main.cpp@0:81c978de0e2b, 2011-09-09 (annotated)
- Committer:
- mimil
- Date:
- Fri Sep 09 13:13:17 2011 +0000
- Revision:
- 0:81c978de0e2b
first release
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mimil | 0:81c978de0e2b | 1 | #include "mbed.h" |
mimil | 0:81c978de0e2b | 2 | #include "picojson.h" |
mimil | 0:81c978de0e2b | 3 | |
mimil | 0:81c978de0e2b | 4 | |
mimil | 0:81c978de0e2b | 5 | void parse() { |
mimil | 0:81c978de0e2b | 6 | picojson::value v; |
mimil | 0:81c978de0e2b | 7 | const char *json = "{\"string\": \"it works\", \"number\": 3.14, \"integer\":5}"; |
mimil | 0:81c978de0e2b | 8 | |
mimil | 0:81c978de0e2b | 9 | string err = picojson::parse(v, json, json + strlen(json)); |
mimil | 0:81c978de0e2b | 10 | printf("res error? %s\r\n", err.c_str()); |
mimil | 0:81c978de0e2b | 11 | printf("string =%s\r\n" , v.get("string").get<string>().c_str()); |
mimil | 0:81c978de0e2b | 12 | printf("number =%f\r\n" , v.get("number").get<double>()); |
mimil | 0:81c978de0e2b | 13 | printf("integer =%d\r\n" , (int)v.get("integer").get<double>()); |
mimil | 0:81c978de0e2b | 14 | } |
mimil | 0:81c978de0e2b | 15 | |
mimil | 0:81c978de0e2b | 16 | void serialize() { |
mimil | 0:81c978de0e2b | 17 | picojson::object v; |
mimil | 0:81c978de0e2b | 18 | picojson::object inner; |
mimil | 0:81c978de0e2b | 19 | string val = "tt"; |
mimil | 0:81c978de0e2b | 20 | |
mimil | 0:81c978de0e2b | 21 | v["aa"] = picojson::value(val); |
mimil | 0:81c978de0e2b | 22 | v["bb"] = picojson::value(1.66); |
mimil | 0:81c978de0e2b | 23 | inner["test"] = picojson::value(true); |
mimil | 0:81c978de0e2b | 24 | inner["integer"] = picojson::value(1.0); |
mimil | 0:81c978de0e2b | 25 | v["inner"] = picojson::value(inner); |
mimil | 0:81c978de0e2b | 26 | |
mimil | 0:81c978de0e2b | 27 | string str = picojson::value(v).serialize(); |
mimil | 0:81c978de0e2b | 28 | printf("serialized content = %s\r\n" , str.c_str()); |
mimil | 0:81c978de0e2b | 29 | } |
mimil | 0:81c978de0e2b | 30 | |
mimil | 0:81c978de0e2b | 31 | void advanced() { |
mimil | 0:81c978de0e2b | 32 | picojson::value v; |
mimil | 0:81c978de0e2b | 33 | const char *jsonsoure = |
mimil | 0:81c978de0e2b | 34 | "{\"menu\": {" |
mimil | 0:81c978de0e2b | 35 | "\"id\": \"f\"," |
mimil | 0:81c978de0e2b | 36 | "\"popup\": {" |
mimil | 0:81c978de0e2b | 37 | " \"menuitem\": [" |
mimil | 0:81c978de0e2b | 38 | " {\"v\": \"0\"}," |
mimil | 0:81c978de0e2b | 39 | " {\"v\": \"1\"}," |
mimil | 0:81c978de0e2b | 40 | " {\"v\": \"2\"}" |
mimil | 0:81c978de0e2b | 41 | " ]" |
mimil | 0:81c978de0e2b | 42 | " }" |
mimil | 0:81c978de0e2b | 43 | "}" |
mimil | 0:81c978de0e2b | 44 | "}"; |
mimil | 0:81c978de0e2b | 45 | char * json = (char*) malloc(strlen(jsonsoure)+1); |
mimil | 0:81c978de0e2b | 46 | strcpy(json, jsonsoure); |
mimil | 0:81c978de0e2b | 47 | string err = picojson::parse(v, json, json + strlen(json)); |
mimil | 0:81c978de0e2b | 48 | printf("res error? %s\r\n", err.c_str()); |
mimil | 0:81c978de0e2b | 49 | |
mimil | 0:81c978de0e2b | 50 | printf("id =%s\r\n", v.get("menu").get("id").get<string>().c_str()); |
mimil | 0:81c978de0e2b | 51 | |
mimil | 0:81c978de0e2b | 52 | picojson::array list = v.get("menu").get("popup").get("menuitem").get<picojson::array>(); |
mimil | 0:81c978de0e2b | 53 | |
mimil | 0:81c978de0e2b | 54 | for (picojson::array::iterator iter = list.begin(); iter != list.end(); ++iter) { |
mimil | 0:81c978de0e2b | 55 | printf("menu item value =%s\r\n", (*iter).get("v").get<string>().c_str()); |
mimil | 0:81c978de0e2b | 56 | } |
mimil | 0:81c978de0e2b | 57 | } |
mimil | 0:81c978de0e2b | 58 | |
mimil | 0:81c978de0e2b | 59 | int main() { |
mimil | 0:81c978de0e2b | 60 | printf("Starting PicoJSONSample "__TIME__"\r\n"); |
mimil | 0:81c978de0e2b | 61 | printf(">>> parsing \r\n"); |
mimil | 0:81c978de0e2b | 62 | parse(); |
mimil | 0:81c978de0e2b | 63 | printf(">>> serializing \r\n"); |
mimil | 0:81c978de0e2b | 64 | serialize(); |
mimil | 0:81c978de0e2b | 65 | printf(">>> advanced parsing \r\n"); |
mimil | 0:81c978de0e2b | 66 | advanced(); |
mimil | 0:81c978de0e2b | 67 | printf("Ending PicoJSONSample " __TIME__ "\r\n"); |
mimil | 0:81c978de0e2b | 68 | } |