将String转换为JSON

时间:2012-10-16 05:12:44

标签: javascript json serialization redis

我是javascript的新手,我正在尝试将redis-cli INFO命令输出转换为JSON。字符串,我试图转换为JSON就像这样

    "redis_version:2.4.8\r\nredis_git_sha1:b85ba5fb\r\nredis_git_dirty:0\r\narch_bits:64\r\nmultiplexing_api:kqueue\r\ngcc_version:4.2.1\r\nprocess_id:6403\r\nuptime_in_seconds:963411\r\nuptime_in_days:11\r\nlru_clock:815387\r\nused_cpu_sys:67.32\r\nused_cpu_user:91.89\r\nused_cpu_sys_children:42.23\r\nused_cpu_user_children:132.57\r\nconnected_clients:2\r\nconnected_slaves:1\r\nclient_longest_output_list:0\r\nclient_biggest_input_buf:0\r\nblocked_clients:0\r\nused_memory:162549360\r\nused_memory_human:155.02M\r\nused_memory_rss:147972096\r\nused_memory_peak:191943360\r\nused_memory_peak_human:183.05M\r\nmem_fragmentation_ratio:0.91\r\nmem_allocator:libc\r\nloading:0\r\naof_enabled:1\r\nchanges_since_last_save:1\r\nbgsave_in_progress:1\r\nlast_save_time:1350325258\r\nbgrewriteaof_in_progress:0\r\ntotal_connections_received:187\r\ntotal_commands_processed:269264\r\nexpired_keys:180\r\nevicted_keys:0\r\nkeyspace_hits:202518\r\nkeyspace_misses:11675\r\npubsub_channels:0\r\npubsub_patterns:0\r\nlatest_fork_usec:2198\r\nvm_enabled:0\r\nrole:master\r\naof_current_size:159904546\r\naof_base_size:158667118\r\naof_pending_rewrite:0\r\naof_buffer_length:0\r\naof_pending_bio_fsync:0\r\nslave0:127.0.0.1,62716,online\r\ndb0:keys=356937,expires=0\r\n"

非常感谢有关如何将此转换为JSON对象的任何指针。谢谢!

3 个答案:

答案 0 :(得分:1)

你可以做这样简单的事情:

​var fields = str.split('\r\n'), result = {};
for(var i = 0, len = fields.length; i < len; ++i) {
    var props = fields[i].split(':');
    if(props.length === 2) {           
        result[props[0]] = props[1];
    }
}

console.log(result);​​​​
console.log(JSON.stringify(result));

我在http://jsfiddle.net/uHtT3/1/创建了一个jsfiddle。

答案 1 :(得分:0)

为什么不将webdis之类的内容用于同一目的?

默认情况下,Webdis回复是包含单个项目的JSON对象,将请求的命令名称映射到Redis回复。使用以下JSON类型:

Redis string → JSON string (GET, LPOP...)
Redis multi-bulk data → JSON list (LRANGE, SMEMBERS...)
Redis integer → JSON integer (INCR...)
Redis status, error → list [bool, string] (PING, SET...)

?jsonp查询字符串参数添加了一个可选的包装函数,将回复转换为JSONP调用。这使得可以在Javascript应用程序中包含Webdis。

答案 2 :(得分:0)

您是否尝试过使用 eval

eval('[{"'+<yourString>+'"}]');

修改

eval('({"'+<yourString>+'"})');

其中json可以是一个字符串,例如:

"a: 21, b:23"

这对我有用

var yourString = "redis_version:2.4.8\r\nredis_git_sha1:b85ba5fb\r\nredis_git_dirty:0\r\narch_bits:64\r\nmultiplexing_api:kqueue\r\ngcc_version:4.2.1\r\nprocess_id:6403\r\nuptime_in_seconds:963411\r\nuptime_in_days:11\r\nlru_clock:815387\r\nused_cpu_sys:67.32\r\nused_cpu_user:91.89\r\nused_cpu_sys_children:42.23\r\nused_cpu_user_children:132.57\r\nconnected_clients:2\r\nconnected_slaves:1\r\nclient_longest_output_list:0\r\nclient_biggest_input_buf:0\r\nblocked_clients:0\r\nused_memory:162549360\r\nused_memory_human:155.02M\r\nused_memory_rss:147972096\r\nused_memory_peak:191943360\r\nused_memory_peak_human:183.05M\r\nmem_fragmentation_ratio:0.91\r\nmem_allocator:libc\r\nloading:0\r\naof_enabled:1\r\nchanges_since_last_save:1\r\nbgsave_in_progress:1\r\nlast_save_time:1350325258\r\nbgrewriteaof_in_progress:0\r\ntotal_connections_received:187\r\ntotal_commands_processed:269264\r\nexpired_keys:180\r\nevicted_keys:0\r\nkeyspace_hits:202518\r\nkeyspace_misses:11675\r\npubsub_channels:0\r\npubsub_patterns:0\r\nlatest_fork_usec:2198\r\nvm_enabled:0\r\nrole:master\r\naof_current_size:159904546\r\naof_base_size:158667118\r\naof_pending_rewrite:0\r\naof_buffer_length:0\r\naof_pending_bio_fsync:0\r\nslave0:127.0.0.1".replace(/\r\n/g, ",").replace(/:/g, ": '").replace(/,/g, "' ,")+"'";
eval('({'+yourString+'})');

或者如果您想避免使用 eval ,请使用 JSON.parse

JSON.parse(yourString);