JSON-C Example(从字符串解析属性)

来源:互联网 发布:c语言continue 编辑:程序博客网 时间:2024/06/05 22:41

原文:http://coolaj86.info/articles/json-c-example.html

So you want to parse JSON with C? Welcome aboard! First get json-c, configure, compile, and update your ld cache

Make JSON-C (libjson)

x86 / Ubuntu

wget http://oss.metaparadigm.com/json-c/json-c-0.9.tar.gz

tar xf json-c-0.9.tar.gz

cd json-c-0.9

./configure && make && sudo make install

sudo ldconfig


ARM / OpenEmbedded

bitbake json-c


Simple Test

Now use one of the test files provided or this example: http://www.jroller.com/RickHigh/entry/json_parsing_with_c_simple

json-example.c:


  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stddef.h>
  4. #include <string.h>
  5. #include <json/json.h>

  6. int main(int argc, char **argv)
  7. {
  8.   struct json_object *new_obj;
  9.   int pageCount;
  10.   new_obj = json_tokener_parse("/* more difficult test case */ { \"glossary\": { \"title\": \"example glossary\", \"pageCount\": 100, \"GlossDiv\": { \"title\": \"S\", \"GlossList\": [ { \"ID\": \"SGML\", \"SortAs\": \"SGML\", \"GlossTerm\": \"Standard Generalized Markup Language\", \"Acronym\": \"SGML\", \"Abbrev\": \"ISO 8879:1986\", \"GlossDef\": \"A meta-markup language, used to create markup languages such as DocBook.\", \"GlossSeeAlso\": [\"GML\", \"XML\", \"markup\"] } ] } } }");

  11.   printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
  12.   new_obj = json_object_object_get(new_obj, "glossary");
  13.   printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
  14.   new_obj = json_object_object_get(new_obj, "pageCount");
  15.   pageCount = json_object_get_int(new_obj);
  16.   printf("Page count = %d", pageCount);
  17.   json_object_put(new_obj);
  18.   return 0;
  19. }

Makefile:

all: static

# if you didn't do `sudo make install` you'll need to add your own paths

# to json.json.h and libjson.so - something like -I/usr/local/include/ -L/usr/local/lib

static:

  gcc -static -o json-example-static json-example.c -ljson

  ./json-example-static

shared:

  gcc -o json-example-shared json-example.c -L/usr/local/lib -ljson

  ./json-example-shared

.PHONY: all static shared


Make json-example

make static
make shared

Hopefully that's enough to get you started. The big thing is that when you compile your own projects, make sure that you include the json library and paths.

Segfaults
If you get a segfault it's most likely that you're accessing a key that doesn't exist.

<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
阅读(1881) | 评论(0) | 转发(1) |
0

上一篇:error C2143: syntax error : missing ';' before 'type'

下一篇:如何用VC读取Excel表格中的数据详细介绍

相关热门文章
  • HTML5 音视频媒体处理相关...
  • 导入mysql文件提示“ASCII '\0...
  • jsp嵌套
  • BLE-NRF51822教程9—动态密码(...
  • nuttx记录2:nuttx几个函数说...
  • test123
  • 编写安全代码——小心有符号数...
  • 使用openssl api进行加密解密...
  • 一段自己打印自己的c程序...
  • sql relay的c++接口
  • linux dhcp peizhi roc
  • 关于Unix文件的软链接
  • 求教这个命令什么意思,我是新...
  • sed -e "/grep/d" 是什么意思...
  • 谁能够帮我解决LINUX 2.6 10...
给主人留下些什么吧!~~
原创粉丝点击