- #include <mjson/json.h>
- #include <stdio.h>
- #include <stdlib.h>
- int main()
- {
- json_t *entry, *root, *head, *body, *label, *value;
- char *document;
- root = json_new_object();
- /*--------Head------*/
- head = json_new_string("head");
- entry = json_new_object();
- label = json_new_string("ID");
- value = json_new_number("1");
- json_insert_child(label,value);
- json_insert_child(entry,label);
- label = json_new_string("Tyep");
- value = json_new_string("0");
- json_insert_child(label,value);
- json_insert_child(entry,label);
- json_insert_child(head, entry);
- /*------End Head------*/
- /*---------Body---------*/
- body = json_new_string("body");
- entry = json_new_object();
- label = json_new_string("phone");
- value = json_new_string("028-2306987");
- json_insert_child(label,value);
- json_insert_child(entry, label);
- label = json_new_string("address");
- value = json_new_string("Los Angeles");
- json_insert_child(label,value);
- json_insert_child(entry,label);
- label = json_new_string("name");
- json_t *array = json_new_array();
- value = json_new_string("Jack");
- json_insert_child(array,value);
- value = json_new_string("Mike");
- json_insert_child(array,value);
- value = json_new_string("Kobe");
- json_insert_child(array,value);
- json_insert_child(label,array);
- json_insert_child(entry,label);
- json_insert_child(body,entry);
- /*------End Body-------*/
- json_insert_child(root,head);
- json_insert_child(root,body);
- json_tree_to_string(root,&document);
- printf("%s\\n", document);
- json_free_value(&root);
- /*---Parsing the documents---*/
- json_parse_document(&root,document);
- head = json_find_first_label(root,"head");
- body = json_find_first_label(root,"body");
- label = json_find_first_label(head->child,"ID");
- printf("%s\\n",label->child->text);
- /*clean up*/
- free(document);
- json_free_value(&root);
- exit(0);
- }
- //该片段来自于http://www.codesnippet.cn/detail/1811201411002.html
来源: http://www.codesnippet.cn/detail/1811201411002.html