赞
踩
c语言生成json在我之前的博客有详细说明,想学习的同学可以跳过去看,这篇博客介绍c语言解析json,和生成过程相反,解析json的目的是从json文件中解析出里面的对象及对象的值。
//aidl接口示例:
interface IMinistro
{
void requestLoader(in IMinistroCallback callback, in Bundle parameters);
}
//aidl接口的json表示:
{
"interface ":
{
"interface_name": "IMinistro",
"function_array":
[
"return_type": "void",
"function_name": "requestLoader",
"param_list":
[
{
"param_tag": "in",
"param_type": "IMinistroCallback",
"param_name": "callback"
}
]
]
}
}
//cJSON结构定义:
/* The cJSON structure: */
typedef struct cJSON
{
/* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */
struct cJSON *next;
struct cJSON *prev;
/* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */
struct cJSON *child;
/* The type of the item, as above. */
int type;
/* The item's string, if type==cJSON_String and type == cJSON_Raw */
char *valuestring;
/* writing to valueint is DEPRECATED, use cJSON_SetNumberValue instead */
int valueint;
/* The item's number, if type==cJSON_Number */
double valuedouble;
/* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
char *string;
} cJSON;
//以下代码是我在gtest工程的测试代码,非常详细,解析json文件用到的接口基本上都有了:
//C语言解析上述json
//out是上述json字符串
//第一步:调用cJSON_Parse开始解析json文件,获得json根节点
cJSON* root = cJSON_Parse(out);
ASSERT_EQ(TRUE, root != NULL);
ASSERT_EQ(TRUE, root->child != NULL);
//第二步,按子节点的名称取出子节点对象interface
cJSON* interface = cJSON_GetObjectItem(root, "interface");
ASSERT_EQ(TRUE, interface != NULL);
ASSERT_EQ(TRUE, cJSON_IsObject(interface));
//从interface 对象中,继续按名称取出其子节点对象interface_name
//通过interface_name->string可以得到节点名
//通过interface_name->valuestring可以得到节点值的字符串表示方式
cJSON* interface_name = cJSON_GetObjectItem(interface, "interface_name");
ASSERT_EQ(TRUE, strcmp(interface_name->string, "interface_name") == 0);
ASSERT_EQ(TRUE, strcmp(interface_name->valuestring, "IMinistro") == 0);
//从interface 对象中,继续按名称取出其子节点对象function_array
//cJSON_IsArray函数可以判断function_array是否是一个数组类型的对象
//cJSON_GetArraySize可以获取数组的大小
cJSON* function_array = cJSON_GetObjectItem(interface, "function_array");
ASSERT_EQ(TRUE, function_array != NULL);
ASSERT_EQ(TRUE, cJSON_IsArray(function_array));
ASSERT_EQ(2, cJSON_GetArraySize(function_array));
//cJSON_GetArrayItem获取对应下标的数组元素
cJSON* function_0 = cJSON_GetArrayItem(function_array, 0);
ASSERT_EQ(TRUE, function_0 != NULL);
ASSERT_EQ(TRUE, cJSON_IsObject(function_0));
//cJSON_GetObjectItem可以获得对象的子对象节点
cJSON* return_type = cJSON_GetObjectItem(function_0, "return_type");
ASSERT_EQ(TRUE, return_type != NULL);
ASSERT_EQ(TRUE, strcmp(return_type->string, "return_type") == 0);
ASSERT_EQ(TRUE, strcmp(return_type->valuestring, "void") == 0);
cJSON* function_name = cJSON_GetObjectItem(function_0, "function_name");
ASSERT_EQ(TRUE, function_name != NULL);
ASSERT_EQ(TRUE, strcmp(function_name->string, "function_name") == 0);
ASSERT_EQ(TRUE, strcmp(function_name->valuestring, "requestLoader") == 0);
cJSON* param_list = cJSON_GetObjectItem(function_0, "param_list");
ASSERT_EQ(TRUE, param_list != NULL);
ASSERT_EQ(TRUE, cJSON_IsArray(param_list));
ASSERT_EQ(2, cJSON_GetArraySize(param_list));
cJSON* param_0 = cJSON_GetArrayItem(param_list, 0);
ASSERT_EQ(TRUE, param_0 != NULL);
ASSERT_EQ(TRUE, cJSON_IsObject(param_0));
cJSON* tag = cJSON_GetObjectItem(param_0, "param_tag");
ASSERT_EQ(TRUE, tag != NULL);
ASSERT_EQ(TRUE, strcmp(tag->string, "param_tag") == 0);
ASSERT_EQ(TRUE, strcmp(tag->valuestring, "in") == 0);
cJSON* param_type = cJSON_GetObjectItem(param_0, "param_type");
ASSERT_EQ(TRUE, param_type != NULL);
ASSERT_EQ(TRUE, strcmp(param_type->string, "param_type") == 0);
ASSERT_EQ(TRUE, strcmp(param_type->valuestring, "IMinistroCallback") == 0);
cJSON* param_name = cJSON_GetObjectItem(param_0, "param_name");
ASSERT_EQ(TRUE, param_name != NULL);
ASSERT_EQ(TRUE, strcmp(param_name->string, "param_name") == 0);
ASSERT_EQ(TRUE, strcmp(param_name->valuestring, "callback") == 0);
//释放资源
cJSON_Delete(root);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。