document

json-c インストール centos ubuntu

c言語でJSONを扱うことになった。

Json-GlibがGNOMEで使われてて評判良さそうだったけど、json-cのほうがテストとかシンプルで、試してみようということで入れてみた。

使い方

基本的には、 json_object_new_object()

でjson用のオブジェクトを作って、

json_object_object_add

で、要素を追加していく。

json_object_to_json_string()

でchar *として出力する

オブジェクトにオブジェクトを作ることもできる

パースはこちらのほうが詳しい

json-c を使ってみる - hidemonの日記 http://d.hatena.ne.jp/hidemon/20080626/1214459877

 //add to object sample
 json_object *test_object = json_object_new_object();
 json_object *test_object_nest = json_object_new_object();
 json_object_object_add(test_object_nest, "nest_1", json_object_new_int(1));
 json_object_object_add(test_object_nest, "nest_2", json_object_new_int(2));
 json_object_object_add(test_object, "abc", json_object_new_int(12));
 json_object_object_add(test_object, "foo", json_object_new_string("bar"));
 json_object_object_add(test_object, "nest_test", test_object_nest);
 printf("my_object.to_string()=%s\n", json_object_to_json_string(test_object));
 json_object_put(test_object);

出力

my_object.to_string()={ "abc": 12, "foo": "bar", "nest_test": { "nest_1": 1, "nest_2": 2 } }

json-c インストール centos

いつものようにyumで見つからない。

Search Results for json-c http://pkgs.org/search/?keyword=json-c&search_on=smart&distro=0&arch=32-bit&exact=0

このサイトで二つのrpm

json-c-0.9-1.el5.i386.rpm - A JSON implementation in C
json-c-devel-0.9-1.el5.i386.rpm - Development headers and library for json-c

をDL

# rpm -ivh json-c-0.9-1.el5.i386.rpm 
# rpm -ivh json-c-devel-0.9-1.el5.i386.rpm 

json-c ubuntu

json-cはubuntuではlibjson0というパッケージ名になっているようだ。

$ sudo apt-get install libjson0 libjson0-dev

参考リンク

jehiah/json-c - GitHub? https://github.com/jehiah/json-c

json-cのプロジェクトページ

json-c を使ってみる - hidemonの日記 http://d.hatena.ne.jp/hidemon/20080626/1214459877

What is the best C++ JSON library? - Quora http://www.quora.com/What-is-the-best-C-JSON-library

JSON http://www.json.org/

JSON http://www.json.org/json-ja.html

JsonGlib? - GNOME Live! http://live.gnome.org/JsonGlib

Sample

勉強用にプロジェクトのGitHub?にあるtest1.cにコメントいれたり、関数追加したりしたコード

	#include <stdio.h>
	#include <stdlib.h>
	#include <stddef.h>
	#include <string.h>
	#include "json.h"
	int main(int argc, char **argv)
	{
	  json_tokener *tok;
	  json_object *my_string, *my_int, *my_object, *my_array;
	  json_object *new_obj;
	  int i;
	  my_int = NULL;
	  my_object = NULL;
	  my_array = NULL;
	  new_obj = NULL;
	  tok = NULL;
	  MC_SET_DEBUG(1);
	  my_string = json_object_new_string("foo");
	  printf("my_string=%s\n", json_object_get_string(my_string));
	  printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string));
	  my_string = json_object_new_string("\t");
	  printf("my_string=%s\n", json_object_get_string(my_string));
	  printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string));
	  json_object_put(my_string);
	  my_int = json_object_new_int(9);
	  printf("my_int=%d\n", json_object_get_int(my_int));
	  printf("my_int.to_string()=%s\n", json_object_to_json_string(my_int));
	  my_array = json_object_new_array();
	  json_object_array_add(my_array, json_object_new_int(1));
	  json_object_array_add(my_array, json_object_new_int(2));
	  json_object_array_add(my_array, json_object_new_int(3));
	  json_object_array_put_idx(my_array, 4, json_object_new_int(5));
	  printf("my_array=\n");
	  for(i=0; i < json_object_array_length(my_array); i++) {
	    json_object *obj = json_object_array_get_idx(my_array, i);
	    printf("\t[%d]=%s\n", i, json_object_to_json_string(obj));
	  }
	  printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array));
	  //add to object sample
	  my_object = json_object_new_object();
	  json_object_object_add(my_object, "abc", json_object_new_int(12));
	  json_object_object_add(my_object, "foo", json_object_new_string("bar"));
	  json_object_object_add(my_object, "bool0", json_object_new_boolean(0));
	  json_object_object_add(my_object, "bool1", json_object_new_boolean(1));
	  json_object_object_add(my_object, "baz", json_object_new_string("bang"));
	  json_object_object_add(my_object, "baz", json_object_new_string("fark"));
	  //delete !!
	  json_object_object_del(my_object, "baz");
	  /*json_object_object_add(my_object, "arr", my_array);*/
	  printf("my_object=\n");
	  json_object_object_foreach(my_object, key, val) {
	    printf("\t%s: %s\n", key, json_object_to_json_string(val));
	  }
	  printf("my_object.to_string()=%s\n", json_object_to_json_string(my_object));
	  //json_tokener_parseでchar *strからJSONにパースする
	  new_obj = json_tokener_parse("\"\003\"");
	  printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
	  json_object_put(new_obj);
	  // \"xxxxx\"で囲まれた文字列しかパースしない??
	  new_obj = json_tokener_parse("/* hello */\"foo\"");
	  printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
	  json_object_put(new_obj);
	  // \"xxxxx\"で囲まれた文字列しかパースしない??
	  new_obj = json_tokener_parse("// hello\n\"foo\"");
	  printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
	  json_object_put(new_obj);
	  // \u0041=A 出力 ABC
	  new_obj = json_tokener_parse("\"\\u0041\\u0042\\u0043\"");
	  printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
	  json_object_put(new_obj);
	  //new_obj.to_string()=null
	  new_obj = json_tokener_parse("null");
	  printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
	  //printf("type of json= %d\n",json_object_get_type(new_obj));
	  json_object_put(new_obj);
	  //大文字でも valueは 真偽のtrueとして認識される?
	  //type of json= 1 = json_type_boolean
	  new_obj = json_tokener_parse("True");
	  printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
	  printf("type of json= %d\n",json_object_get_type(new_obj));
	  json_object_put(new_obj);
	  //intとして認識
	  new_obj = json_tokener_parse("12");
	  printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
	  printf("type of json= %d\n",json_object_get_type(new_obj));
	  json_object_put(new_obj);
	  //doubleとして認識
	  new_obj = json_tokener_parse("12.3");
	  printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
	  printf("type of json= %d\n",json_object_get_type(new_obj));
	  json_object_put(new_obj);
	  //arrayとして認識
	  new_obj = json_tokener_parse("[\"\\n\"]");
	  printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
	  printf("type of json= %d\n",json_object_get_type(new_obj));
	  printf("array length= %d\n",json_object_array_length(new_obj));
	  json_object_put(new_obj);
	  //arrayとして認識
	  new_obj = json_tokener_parse("[\"\\nabc\\n\"]");
	  printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
	  printf("type of json= %d\n",json_object_get_type(new_obj));
	  printf("array length= %d\n",json_object_array_length(new_obj));
	  json_object_put(new_obj);
	  //arrayとして認識
	  new_obj = json_tokener_parse("[null]");
	  printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
	  printf("type of json= %d\n",json_object_get_type(new_obj));
	  printf("array length= %d\n",json_object_array_length(new_obj));
	  json_object_put(new_obj);
	  //arrayとして認識
	  //array length = 0
	  new_obj = json_tokener_parse("[]");
	  printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
	  printf("type of json= %d\n",json_object_get_type(new_obj));
	  printf("array length= %d\n",json_object_array_length(new_obj));
	  json_object_put(new_obj);
	  //arrayとして認識
	  new_obj = json_tokener_parse("[false]");
	  printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
	  printf("type of json= %d\n",json_object_get_type(new_obj));
	  printf("array length= %d\n",json_object_array_length(new_obj));
	  json_object_put(new_obj);
	  //array length= 4
	  new_obj = json_tokener_parse("[\"abc\",null,\"def\",12]");
	  printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
	  printf("type of json= %d\n",json_object_get_type(new_obj));
	  printf("array length= %d\n",json_object_array_length(new_obj));
	  json_object_put(new_obj);
	  //objectとして認識
	  new_obj = json_tokener_parse("{}");
	  printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
	  printf("type of json= %d\n",json_object_get_type(new_obj));
	  printf("array length= %d\n",json_object_array_length(new_obj));
	  json_object_put(new_obj);
	  //objectとして認識
	  //array length= 1
	  new_obj = json_tokener_parse("{ \"foo\": \"bar\" }");
	  printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
	  printf("type of json= %d\n",json_object_get_type(new_obj));
	  printf("array length= %d\n",json_object_array_length(new_obj));
	  json_object_put(new_obj);
	  //objectとして認識
	  //array length= 3
	  new_obj = json_tokener_parse("{ \"foo\": \"bar\", \"baz\": null, \"bool0\": true }");
	  printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
	  printf("type of json= %d\n",json_object_get_type(new_obj));
	  printf("array length= %d\n",json_object_array_length(new_obj));
	  json_object_put(new_obj);
	  //objectとして認識
	  //array length= 1
	  new_obj = json_tokener_parse("{ \"foo\": [null, \"foo\"] }");
	  printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
	  printf("type of json= %d\n",json_object_get_type(new_obj));
	  printf("array length= %d\n",json_object_array_length(new_obj));
	  //printf("array_get_idx = 0 =>array length= %d\n",json_object_array_length(json_object_array_get_idx(new_obj,1)));
	  //printf("array_get_idx = 0 =>array length= %d\n",new_obj->o.c_array->length);
	  json_object_put(new_obj);
	  new_obj = json_tokener_parse("{ \"abc\": 12, \"foo\": \"bar\", \"bool0\": false, \"bool1\": true, \"arr\": [ 1, 2, 3, null, 5 ] }");
	  printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
	  printf("type of json= %d\n",json_object_get_type(new_obj));
	  printf("array length= %d\n",json_object_array_length(new_obj));
	  json_object_put(new_obj);
	  new_obj = json_tokener_parse("{ foo }");
	  if(is_error(new_obj)) printf("got error as expected\n");
	  new_obj = json_tokener_parse("foo");
	  if(is_error(new_obj)) printf("got error as expected\n");
	  new_obj = json_tokener_parse("{ \"foo");
	  if(is_error(new_obj)) printf("got error as expected\n");
	  /* test incremental parsing */
	  tok = json_tokener_new();
	  new_obj = json_tokener_parse_ex(tok, "{ \"foo", 6);
	  if(is_error(new_obj)) printf("got error as expected json_tokener_parse_ex\n");
	  new_obj = json_tokener_parse_ex(tok, "\": {\"bar", 8);
	  if(is_error(new_obj)) printf("got error as expected json_tokener_parse_ex\n");
	  // \"から始まって、 追加で\”を使って終了すれば、JSON形式として認識される
	  new_obj = json_tokener_parse_ex(tok, "\":13}}", 6);
	  printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
	  json_object_put(new_obj);
	  json_tokener_free(tok);
	  json_object_put(my_string);
	  json_object_put(my_int);
	  json_object_put(my_object);
	  json_object_put(my_array);
	  //add to nest object sample
	  json_object *test_object = json_object_new_object();
	  json_object *test_object_nest = json_object_new_object();
	  json_object_object_add(test_object_nest, "nest_1", json_object_new_int(1));
	  json_object_object_add(test_object_nest, "nest_2", json_object_new_int(2));
	  json_object_object_add(test_object, "abc", json_object_new_int(12));
	  json_object_object_add(test_object, "foo", json_object_new_string("bar"));
	  json_object_object_add(test_object, "nest_test", test_object_nest);
	  //delete !!
	  //json_object_object_del(my_object, "baz");
	  /*json_object_object_add(my_object, "arr", my_array);*/
	  printf("my_object=\n");
	  json_object_object_foreach(test_object, key1, val1) {
	    printf("\t%s: %s\n", key1, json_object_to_json_string(val1));
	  }
	  printf("my_object.to_string()=%s\n", json_object_to_json_string(test_object));
	  json_object_put(test_object);
	  return 0;
	}

自作プログラムのコンパイル

json-cはmakefile.amで作ってたり、 libtoolみたいな変なスクリプトを使ってコンパイルしてた。

自作したファイルをサクッと試したい時のコンパイルコマンド

ただしjson-cと同じディレクトリにファイルを作ってそこで実行。
ファイル名はmy_test.c
	gcc -DHAVE_CONFIG_H -I.    -Wall -Wwrite-strings -Werror -std=gnu99 -D_GNU_SOURCE -D_REENTRANT -g -O2 -MT test1.o -MD -MP -MF  .deps/my_test.Tpo -c -o my_test.o my_test.c
	mv -f .deps/my_test.Tpo .deps/my_test.Po
	gcc -Wall -Wwrite-strings -Werror -std=gnu99 -D_GNU_SOURCE -D_REENTRANT -g -O2 -o /my_test my_test.o  ./.libs/libjson.so

はまったこと

まだ使うオブジェクトを先にputするとエラーになる

 json_object_object_add(c_json_object, "child", json_object_new_int(0));
 json_object_object_add(p_json_object, "parent", c_json_object);
//これいれるとエラーになる
//Segmentation fault
json_object_put(c_json_object);
printf("my_object.to_string()=%s\n", json_object_to_json_string(p_json_object));

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2011-11-03 (木) 16:00:42 (2786d)