| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
jopen
10年前发布

C编写的轻量级json库:parson

parson是一个采用C编写的轻量级JOSN库。

特性

  • Full JSON support
  • Lightweight (only 2 files)
  • Simple API
  • Addressing json values with dot notation (similiar to C structs or objects in most OO languages, e.g. "objectA.objectB.value")
  • C89 compatible
  • Test suites

解析JSON

Here is a function, which prints basic commit info (date, sha and author) from a github repository.

void print_commits_info(const char *username, const char *repo) {      JSON_Value *root_value;      JSON_Array *commits;      JSON_Object *commit;      size_t i;        char curl_command[512];      char cleanup_command[256];      char output_filename[] = "commits.json";        /* it ain't pretty, but it's not a libcurl tutorial */      sprintf(curl_command,           "curl -s \"https://api.github.com/repos/%s/%s/commits\" > %s",          username, repo, output_filename);      sprintf(cleanup_command, "rm -f %s", output_filename);      system(curl_command);        /* parsing json and validating output */      root_value = json_parse_file(output_filename);      if (json_value_get_type(root_value) != JSONArray) {          system(cleanup_command);          return;      }        /* getting array from root value and printing commit info */      commits = json_value_get_array(root_value);      printf("%-10.10s %-10.10s %s\n", "Date", "SHA", "Author");      for (i = 0; i < json_array_get_count(commits); i++) {          commit = json_array_get_object(commits, i);          printf("%.10s %.10s %s\n",                 json_object_dotget_string(commit, "commit.author.date"),                 json_object_get_string(commit, "sha"),                 json_object_dotget_string(commit, "commit.author.name"));      }        /* cleanup code */      json_value_free(root_value);      system(cleanup_command);  }

Calling print_commits_info("torvalds", "linux"); prints:

Date       SHA        Author  2012-10-15 dd8e8c4a2c David Rientjes  2012-10-15 3ce9e53e78 Michal Marek  2012-10-14 29bb4cc5e0 Randy Dunlap  2012-10-15 325adeb55e Ralf Baechle  2012-10-14 68687c842c Russell King  2012-10-14 ddffeb8c4d Linus Torvalds

项目主页:http://www.open-open.com/lib/view/home/1416923425456

 本文由用户 jopen 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
 转载本站原创文章,请注明出处,并保留原始链接、图片水印。
 本站是一个以用户分享为主的开源技术平台,欢迎各类分享!
 本文地址:https://www.open-open.com/lib/view/open1416923425456.html
parson JSON开发包