1

我正在使用 mjson 库(http://mjson.sourceforge.net/ ) 来做一些简单的 json 编码。json 输出应该类似于

{“类型”:“我的类型”,“主机名”:“我的主机名”...“状态”:“我的状态”}

我的函数 return {},或者基于这个例子: http: //mjson.sourceforge.net/examples.html 我让函数 return state : "state : "myState" { "type" : "mytype" ....,状态在数组之外。

这是我的编码功能:

json_t *make_LogEntryToJson( NAGIOS_LOGENTRY *logEntry )
{

    json_t *entry, *label, *value ;

    // create an entry node
    entry = json_new_object();

    // insert the first label-value pair
    label = json_new_string("type");
    value = json_new_string( logEntry -> type );
    json_insert_child( label, value);
    json_insert_child(entry, label );

    // insert the second label-value pair
    label = json_new_string("hostname");
    value = json_new_string( logEntry -> hostname );
    json_insert_child( label, value);
    json_insert_child(entry, label );

    // insert the second label-value pair
    label = json_new_string("srvname");
    value = json_new_string(logEntry -> srvname);
    json_insert_child( label, value);
    json_insert_child(entry, label );

    // insert the second label-value pair
    label = json_new_string("libelle");
    value = json_new_string( logEntry -> libelle );
    json_insert_child( label, value);
    json_insert_child(entry, label );

    // insert the second label-value pair
    label = json_new_string("timestamp");
    value = json_new_string( logEntry -> timestamp );
    json_insert_child( label, value);
    json_insert_child(entry, label );

    // insert the second label-value pair
    label = json_new_string("stateType");
    value = json_new_string( logEntry -> stateType );
    json_insert_child( label, value);
    json_insert_child(entry, label );

    // insert the second label-value pair
    label = json_new_string("state");
    value = json_new_string( logEntry -> state );
    json_insert_child( label, value);
    json_insert_child(entry, label );

    return entry ;
}

任何帮助将不胜感激 !

4

0 回答 0