使用YAJL解析C中的复杂JSON子对象 [英] Parse complex JSON sub objects in C with YAJL

查看:192
本文介绍了使用YAJL解析C中的复杂JSON子对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有YAJL来解析我简单的元素,如包含的示例中给出的那样,没有问题. (字符串,整数,数组等)

I have YAJL parsing me simple elements like given in the included example without a problem. (strings, integers, arrays, ...)

示例代码可在此处找到: http://lloyd.github.io/yajl/yajl-2.0.1/example_2parse_config_8c-example.html

The example code can be found here: http://lloyd.github.io/yajl/yajl-2.0.1/example_2parse_config_8c-example.html

但是现在我有了这种JSON对象:

but now I have this type of JSON object:

{
"cmd":2,
"properties":
    [
        {
        "idx":40,
        "val":8813.602692
        },
        {
        "idx":41,
        "val":960
        },
        {
        "idx":42,
        "val":2
        },
        {
        "idx":48,
        "val":9
        }
    ]

}

我可以使用检索命令(请参见链接的示例中使用的变量的定义):

I can retrieve the command with (see the definitions of used variables in the linked example):

const char * path[] = {"cmd", (const char *) 0 };
yajl_val v = yajl_tree_get(ynode, path, yajl_t_number);
if (v)
  *cmd = (commands)((int)YAJL_GET_INTEGER(v));

我可以使用以下方法获取对属性数组的引用:

And I can get the reference to the properties array using:

int ar_sz;
const char * path[] = {"properties", (const char *) 0 };
yajl_val v = yajl_tree_get(ynode, path, yajl_t_array);
if (v)
  {
  ar_sz = v->u.array.len;
  }

它为我提供了正确的数组大小,但是我不知道如何从数组元素中检索嵌套元素idx和val.

It gives me the correct array size, but I have no clue on how to retrieve the nested elements idx and val from the array elements.

非常感谢您的帮助

推荐答案

通过查看yajl_tree.h特别是yajl_val_s结构:

{
    const char * path[] = { "properties", (const char *) 0 };
    yajl_val v = yajl_tree_get( node, path, yajl_t_array );
    if ( v && YAJL_IS_ARRAY( v ) ) {
        printf( "is array\n" );

        // iterate over array elements,
        // it's an array so access array.len inside yajl_val_s.union
        size_t len = v->u.array.len;
        int i;
        for ( i = 0; i < len; ++i ) {

            // get ref to one object in array at a time
            yajl_val obj = v->u.array.values[ i ]; // object
            // iterate over values in object: pairs of (key,value)
            // u.object.len tells you number of elements
            size_t nelem = obj->u.object.len;
            int ii;
            for ( ii = 0; ii < nelem; ++ii ) {
                // key is just char *
                const char * key = obj->u.object.keys[ ii ];     // key
                // values is an array object
                yajl_val val = obj->u.object.values[ ii ];       // val
               // example: check if double,
               // could do more checks or switch on value ...
               if ( YAJL_IS_DOUBLE( val ) )
                    printf( "%s/%f ", key, val->u.number.d );
            }
            printf( "\n" );
        }
    } else { printf( "is not array\n" ); }
}

输出应类似于:

is array
idx/40.000000 val/8813.602692 
idx/41.000000 val/960.000000 
idx/42.000000 val/2.000000 
idx/48.000000 val/9.000000 

这篇关于使用YAJL解析C中的复杂JSON子对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆