Linux下C语言操作json数据

本文最后更新于2017年8月19日,已超过 1 年没有更新,如果文章内容失效,还请反馈给我,谢谢!

=Start=

缘由:

总结、提高需要

正文:

参考解答:
#include <stdio.h>
#include <json-c/json.h>

/*
{
  Name: haha,
  Id:   101,
  Age:  21,
  info:{
    number:  1,
    score:  91,
    type:    2,
    params: ""
  }
}
*/

// gcc json-c_parse_string.c -ljson-c
int main(int argc, char const *argv[])
{
  /* Declaring the json data's in json format. */
  char buf[] = "{ \"Name\": \"haha\", \"Id\": 101, \"Age\": 21, \"info\": { \"number\": 1, \"score\": 91, \"type\": 2, \"params\": \"w\" } }";

  /* Declaring the json_object. To pass the Json string to the newly created json_object. */
  json_object *new_obj = json_tokener_parse(buf);
  if (!new_obj)
    return -1;

  json_object *val_obj = NULL;
  json_object *result = NULL;
  const char *str = NULL;

  /* To get the data's then we have to get to the specific node by using the below function. */

  if( json_object_object_get_ex(new_obj, "Name", &val_obj) ) {
    str = json_object_get_string(val_obj);
    printf("Name: %s\n", str);
  }
  if( json_object_object_get_ex(new_obj, "Id", &val_obj) ) {
    str = json_object_get_string(val_obj);
    printf("Id: %s\n", str);
  }
  if( json_object_object_get_ex(new_obj, "Age", &val_obj) ) {
    str = json_object_get_string(val_obj);
    printf("Age: %s\n", str);
  }

  if( json_object_object_get_ex(new_obj, "info", &val_obj) ) {
    if( json_object_object_get_ex(val_obj, "number", &result) ) {
      printf("\tinfo -> number: %d\n", json_object_get_int(result));
    }
    if( json_object_object_get_ex(val_obj, "score", &result) ) {
      printf("\tinfo -> score: %d\n", json_object_get_int(result));
    }
    if( json_object_object_get_ex(val_obj, "type", &result) ) {
      printf("\tinfo -> type: %d\n", json_object_get_int(result));
    }
    if( json_object_object_get_ex(val_obj, "params", &result) ) {
      printf("\tinfo -> params: %s\n", json_object_get_string(result));
    }
  }

  json_object_put(new_obj); // to return the pointer to its originalobjects

  return 0;
}

&

#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <json-c/json.h>

#ifdef TEST_FORMATTED
#define json_object_to_json_string(obj) json_object_to_json_string_ext(obj, sflags)
#else
/* no special define */
#endif

// gcc json_object_to_json_string.c -ljson-c -DTEST_FORMATTED=2
int main(int argc, char **argv)
{
	json_object *new_obj;
#ifdef TEST_FORMATTED
	int sflags = TEST_FORMATTED;
#endif

	new_obj = json_tokener_parse("/* more difficult test case */"
				     "{ \"glossary\": { \"title\": \"example glossary\", \"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\"] } ] } } }");
	printf("new_obj.to_string() = %s\n", json_object_to_json_string(new_obj));
	json_object_put(new_obj);

	return 0;
}

json_object_new_object();
json_object_new_int();
json_object_new_string();
json_object_object_add();

json_object_to_json_string();

json_tokener_parse();
json_object_object_get_ex();
json_object_get_int();
json_object_get_string();

json_object_put();

参考链接:

json-c
https://github.com/json-c/json-c
https://github.com/json-c/json-c/tree/master/tests
https://json-c.github.io/json-c/json-c-0.12/doc/html/json__object_8h.html #官方文档

JSON C语言API整理 #nice
http://7player.cn/2015/01/03/json-c%E8%AF%AD%E8%A8%80api%E6%95%B4%E7%90%86/

Parsing JSON using C [closed]
https://stackoverflow.com/questions/6673936/parsing-json-using-c

How to extract value of json object key, value pairs using json-c
https://stackoverflow.com/questions/29483115/how-to-extract-value-of-json-object-key-value-pairs-using-json-c

=END=

声明: 除非注明,ixyzero.com文章均为原创,转载请以链接形式标明本文地址,谢谢!
https://ixyzero.com/blog/archives/3559.html

One thought on “Linux下C语言操作json数据”

发表评论

电子邮件地址不会被公开。 必填项已用*标注