这样都实现后, 字符串解析为 json 对象就实现了。
json 序列化也成为把 json 输出出来。
一般有两种输出:格式化输出,压缩输出。
简单的说就是要不要输出一些空白的问题。c 链表的定义和使用
/* Render a cJSON item/entity/structure to text. */
char *cJSON_Print(cJSON *item) {
return print_value(item,0,1);
}
char *cJSON_PrintUnformatted(cJSON *item) {
return print_value(item,0,0);
}
/* Render a value to text. */
static char *print_value(cJSON *item,int depth,int fmt) {
char *out=0;
if (!item) return 0;
switch ((item->type)&255) {
case cJSON_NULL:
out=cJSON_strdup("null");
break;
case cJSON_False:
out=cJSON_strdup("false");
break;
case cJSON_True:
out=cJSON_strdup("true");
break;
case cJSON_Number:
out=print_number(item);
break;
case cJSON_String:
out=print_string(item);
break;
case cJSON_Array:
out=print_array(item,depth,fmt);
break;
case cJSON_Object:
out=print_object(item,depth,fmt);
break;
}
return out;
}
由于基本类型输出的实现比较简单,这里就不多说了,这里只说说输出 对象的实现吧。
假设我们要使用格式化输出, 也就是美化输出。
cjson 的做法不是边分析 json 边输出, 而是预先将要输的内容全部按字符串存在内存中, 最后输出整个字符串。
这对于比较大的 json 来说, 内存就是个问题了。
另外,格式化输出依靠的是节点的深度, 这个也可以优化, 一般宽度超过80 时, 就需要从新的一行算起的。
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-57098-10.html
马云这话不对
而更多的国家就看到美国在世界的霸权是多么让人讨厌
这个纯粹胡说