void encodeJson();
void decodeJson();
int main(int argc, char **argv)
{
int ret = 0;
encodeJson();
decodeJson();
return ret;
}
static char *json_type_to_name(int json_type)
{
char json_type_name[128];
memset(json_type_name, 0x00, sizeof(json_type_name));
switch(json_type)
{
case json_type_null:
snprintf(json_type_name, sizeof(json_type_name), "%s", "json_type_null");
break;
case json_type_boolean:
snprintf(json_type_name, sizeof(json_type_name), "%s", "json_type_boolean");
break;
case json_type_double:
snprintf(json_type_name, sizeof(json_type_name), "%s", "json_type_double");
break;
case json_type_int:
snprintf(json_type_name, sizeof(json_type_name), "%s", "json_type_int");
break;
case json_type_object:
snprintf(json_type_name, sizeof(json_type_name), "%s", "json_type_object");
break;
case json_type_array:
snprintf(json_type_name, sizeof(json_type_name), "%s", "json_type_array");
break;
case json_type_string:
snprintf(json_type_name, sizeof(json_type_name), "%s", "json_type_string");
break;
}
return json_type_name;
}
static void getFieldValue(struct json_object *child_obj, const char *field)
{
struct json_object *obj = json_object_object_get(child_obj, field);
enum json_type obj_type = json_object_get_type(obj);
//printf("%s json_type is %s\n", field, json_type_to_name(obj_type));
if (obj_type == json_type_int)
{
printf("%s=%d\n", field, json_object_get_int(obj));
}
else if (obj_type == json_type_string)
{
printf("%s=%s\n", field, json_object_get_string(obj));
}
}
void decodeJson()
{
const char *json_string = "{ \n\
\"userinfo\": [ \n\
{ \"pin\": \"10000\", \"cardno\": \"123456789\", \"password\": \"123456\", \"name\": \"paul\", \"Privilege\": 14, \"flag\": 1 }, \n\
{ \"pin\": \"10001\", \"cardno\": \"123456789\", \"password\": \"123456\", \"name\": \"paul\", \"Privilege\": 14, \"flag\": 1 }, \n\
{ \"pin\": \"10002\", \"cardno\": \"123456789\", \"password\": \"123456\", \"name\": \"paul\", \"Privilege\": 14, \"flag\": 1 }, \n\
{ \"pin\": \"10003\", \"cardno\": \"123456789\", \"password\": \"123456\", \"name\": \"paul\", \"Privilege\": 14, \"flag\": 1 }, \n\
{ \"pin\": \"10004\", \"cardno\": \"123456789\", \"password\": \"123456\", \"name\": \"paul\", \"Privilege\": 14, \"flag\": 1 }, \n\
{ \"pin\": \"10005\", \"cardno\": \"123456789\", \"password\": \"123456\", \"name\": \"paul\", \"Privilege\": 14, \"flag\": 1 }, \n\
{ \"pin\": \"10006\", \"cardno\": \"123456789\", \"password\": \"123456\", \"name\": \"paul\", \"Privilege\": 14, \"flag\": 1 }, \n\
{ \"pin\": \"10007\", \"cardno\": \"123456789\", \"password\": \"123456\", \"name\": \"paul\", \"Privilege\": 14, \"flag\": 1 }, \n\
{ \"pin\": \"10008\", \"cardno\": \"123456789\", \"password\": \"123456\", \"name\": \"paul\", \"Privilege\": 14, \"flag\": 1 }, \n\
{ \"pin\": \"10009\", \"cardno\": \"123456789\", \"password\": \"123456\", \"name\": \"paul\", \"Privilege\": 14, \"flag\": 1 } \n\
] }";
int ret = 0;
int i = 0;
struct json_object *root_obj = NULL;
struct json_object *arry_obj = NULL;
struct json_object *child_obj = NULL;
struct lh_entry *entry = NULL;
char *key = NULL;
root_obj = json_tokener_parse(json_string);
entry = json_object_get_object(root_obj)->head;
if (entry)
{
key = (char *)entry->k;
arry_obj = (struct json_object *)entry->v;
if (json_type_array != json_object_get_type(arry_obj)
&& json_type_object != json_object_get_type(arry_obj))
{
return -1;
}
printf("key = %s\n", key);
printf("array_obj = %s\n", json_object_to_json_string(arry_obj));
if (json_type_array == json_object_get_type(arry_obj))
{
for(i = 0; i < json_object_array_length(arry_obj); i++)
{
child_obj = json_object_array_get_idx(arry_obj, i);
//printf("child_obj = %s\n", json_object_to_json_string(child_obj));
getFieldValue(child_obj, "pin");
getFieldValue(child_obj, "cardno");
getFieldValue(child_obj, "password");
getFieldValue(child_obj, "name");
getFieldValue(child_obj, "Privilege");
getFieldValue(child_obj, "flag");
printf("\n");
}
}
else if (json_type_object == json_object_get_type(arry_obj))
{
}
}
}
void encodeJson()
{
int i = 0;
struct json_object *root_obj = NULL;
struct json_object *child_obj = NULL;
struct json_object *array_obj = NULL;
char *tmpStr = NULL;
root_obj = json_object_new_object();
array_obj = json_object_new_array();
for(i = 0; i < 10; i++)
{
child_obj = json_object_new_object();
json_object_object_add(child_obj, "pin", json_object_new_string("10000"));
json_object_object_add(child_obj, "cardno", json_object_new_string("123456789"));
json_object_object_add(child_obj, "password", json_object_new_string("123456"));
json_object_object_add(child_obj, "name", json_object_new_string("paul"));
json_object_object_add(child_obj, "Privilege", json_object_new_int(14));
json_object_object_add(child_obj, "flag", json_object_new_int(1));
json_object_array_add(array_obj, child_obj);
child_obj = NULL;
}
json_object_object_add(root_obj, "userinfo", array_obj);
tmpStr = json_object_to_json_string(root_obj);
printf("%s\n", tmpStr);
}