如何将byte []响应转换为列表或字典或对象? [英] How to convert the byte[] response to a list or dictionary or object?

查看:57
本文介绍了如何将byte []响应转换为列表或字典或对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到针对redis服务器的命令-sentinel主人的响应。



 public string GetMasterFromSentinel(string sentinelAddress)
{
TcpClient服务器;

try
{
var splittedAddress = sentinelAddress.Split(':');
server = new TcpClient(splittedAddress [0],int.Parse(splittedAddress [1]));
}
catch(SocketException)
{
// _log.Error(无法连接到服务器);
返回string.Empty;
}
NetworkStream ns = server.GetStream();
var payload = new byte [] {0x2a,0x32,0x0d,0x0a,0x24,0x38,0x0d,0x0a,0x73,0x65,0x6e,0x74,0x69,0x6e,0x65,0x6c,
0x0d, 0x0a,0x24,0x37,0x0d,0x0a,0x6d,0x61,0x73,0x74,0x65,0x72,0x73,0x0d,0x0a};
ns.Write(payload,0,payload.Length);
ns.Flush();

var data = new byte [1024];
ns.Read(data,0,data.Length);

ns.Close();
server.Close();
string output = string.Empty;

foreach(数据中的字节b)
输出+ =(字符)b;

//我已经从字符串中删除空值
output = output.Replace(\\\\ n,string.Empty).Replace($,| );

返回输出;
}





删除空值前的输出值:* 1 \\\\ n * 40 * \ r \\ n \
$ 4\r\\\
name\r\\\
$ 8\r\\\
mymaster\r\\\
$ 2\r\\\
ip\r\\\
$ 12\r\\\
10.93.247 .96\r\\\
$ 4\r\\\
port\r\\\
$ 4\r\\\
6379\r\\\
$ 5\r\\\
runid\r\\\
$ 40\ r\\\
03ba9934d84efe28e583f4dca03f5b6c710bd712\r\\\
$ 5\r\\\
flags\r\\\
$ 6\r\\\
master\r\\\
$ 21\r\\\
link待批,commands\r \\\
$ 1\r\\\
0\r\\\
$ 13\r\\\
link-refcount\r\\\
$ 1\r\\\
1\r\\\
$ 14\r\ n上次平-sent\r\\\
$ 1\r\\\
0\r\\\
$ 18\r\\\
last-OK-PING-reply\r\\\
$ 3\r\\\
134 \r\\\
$ 15\r\\\
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\0\0\0\0\0\0\0\\ \\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0 \0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\ 0\0\0\0\0\0\0\0\0\0 \0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\ 0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\\ \\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0 \0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\ 0 \\\\\\\\\



删除空值后的输出值:

* 1 * 40 $ 4name $ 8mymaster $ 2IP $ 1210.73.247.36 $ 4PORT $四六三七九$ 5runid $ 4003ba9934d84efe28e583f4dca03f5b6c710bd712 $ 5flags $ 6master $ 21link未决-命令$ 10 $ 13link-的refcount $ $ 11 14last平发送的$ 10 $ 18last-OK平回复$ 3958 $ 15last平,回复$ 3958 $ 23down-后毫秒$ 510000 $ 12info,刷新$四万八千六百八十九$ 13role报$ 6master $ 18role报告的时间$八七六八七九九五三$ 12config历元$ $ 16 10num奴隶$ $ 11 19num - 其他 - 哨兵$ 11 $ 6quorum $ 12 $ 16failove R-超时$六一八○○○○$ 14parallel-同步$ 11\0\0\0\0\0\0\0\0\0\0\0\0\0\ 0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\\ \\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0 \0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\ 0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\\ \\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0 \0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\ 0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\\ \\0\0\0\0\0\0\0\\ 0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\\ \\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0 \0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\ 0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\\ \\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0 \0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\ 0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\\ \\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0 \0\0\0\0\0\0\0\0\0\\ \\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0 \0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\ 0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\\ \\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0 \0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\ 0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\\ \\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0 \0\0\0\0\0





我需要转换相应的键和值字典或对象。



我有什么试过:



我试过从输出中删除空值。

是否有正确的方法来过滤/解析发送的信息通过redis服务器?

Redis的实际响应如下:

1)1)name

2)resque

3)ip

4)127.0.0.1

5)port

6) 7789

...

解决方案

,|);

返回输出;
}





删除空值前的输出值:* 1 \\\\ n * 40 * \ r \\ n \


4\r\\\
name\r\\\

8\r\\\
mymaster\r\\\

I am getting response for the command -sentinel masters against redis server.

public string GetMasterFromSentinel(string sentinelAddress)
        {
            TcpClient server;

            try
            {
                var splittedAddress = sentinelAddress.Split(':');
                server = new TcpClient(splittedAddress[0], int.Parse(splittedAddress[1]));
            }
            catch (SocketException)
            {
                // _log.Error("Unable to connect to server");
                return string.Empty;
            }
            NetworkStream ns = server.GetStream();
            var payload = new byte[] { 0x2a, 0x32, 0x0d, 0x0a, 0x24, 0x38, 0x0d, 0x0a, 0x73, 0x65, 0x6e, 0x74, 0x69, 0x6e, 0x65, 0x6c, 
                0x0d, 0x0a, 0x24, 0x37, 0x0d, 0x0a, 0x6d, 0x61, 0x73, 0x74, 0x65, 0x72, 0x73, 0x0d, 0x0a };
            ns.Write(payload, 0, payload.Length);
            ns.Flush();

            var data = new byte[1024];
            ns.Read(data, 0, data.Length);

            ns.Close();
            server.Close();
            string output = string.Empty;
           
            foreach (byte b in data)
                output += (Char)b;
           
           //i have put remove nulls from the string
            output = output.Replace("\r\n", string.Empty).Replace("$","|");

            return output;
        }



Value of output before removing nulls:"*1\r\n*40\r\n$4\r\nname\r\n$8\r\nmymaster\r\n$2\r\nip\r\n$12\r\n10.93.247.96\r\n$4\r\nport\r\n$4\r\n6379\r\n$5\r\nrunid\r\n$40\r\n03ba9934d84efe28e583f4dca03f5b6c710bd712\r\n$5\r\nflags\r\n$6\r\nmaster\r\n$21\r\nlink-pending-commands\r\n$1\r\n0\r\n$13\r\nlink-refcount\r\n$1\r\n1\r\n$14\r\nlast-ping-sent\r\n$1\r\n0\r\n$18\r\nlast-ok-ping-reply\r\n$3\r\n134\r\n$15\r\nlast-ping-reply\r\n$3\r\n134\r\n$23\r\ndown-after-milliseconds\r\n$5\r\n10000\r\n$12\r\ninfo-refresh\r\n$4\r\n6802\r\n$13\r\nrole-reported\r\n$6\r\nmaster\r\n$18\r\nrole-reported-time\r\n$8\r\n76627249\r\n$12\r\nconfig-epoch\r\n$1\r\n6\r\n$10\r\nnum-slaves\r\n$1\r\n1\r\n$19\r\nnum-other-sentinels\r\n$1\r\n1\r\n$6\r\nquorum\r\n$1\r\n2\r\n$16\r\nfailover-timeout\r\n$6\r\n180000\r\n$14\r\nparallel-syncs\r\n$1\r\n1\r\n\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"

Value of Output after removing nulls:
"*1*40$4name$8mymaster$2ip$1210.73.247.36$4port$46379$5runid$4003ba9934d84efe28e583f4dca03f5b6c710bd712$5flags$6master$21link-pending-commands$10$13link-refcount$11$14last-ping-sent$10$18last-ok-ping-reply$3958$15last-ping-reply$3958$23down-after-milliseconds$510000$12info-refresh$48689$13role-reported$6master$18role-reported-time$876879953$12config-epoch$16$10num-slaves$11$19num-other-sentinels$11$6quorum$12$16failover-timeout$6180000$14parallel-syncs$11\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"


I need to convert the respective key and values in a dictionary or object.

What I have tried:

I have tried removing the nulls from output.
Is there proper way to filter/parse the information sent by redis server?
The actual response from Redis will be like:
1) 1) "name"
2) "resque"
3) "ip"
4) "127.0.0.1"
5) "port"
6) "7789"
...

解决方案

","|"); return output; }



Value of output before removing nulls:"*1\r\n*40\r\n


4\r\nname\r\n


8\r\nmymaster\r\n


这篇关于如何将byte []响应转换为列表或字典或对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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