将jarray转换为c#中的字节数组

时间:2014-03-18 11:15:20

标签: c# asp.net asp.net-mvc c#-4.0 json.net

我需要以某种方式将jarray转换为c#中的字节数组,以便我可以显示相应的图像。呼叫是通过RESTful Web服务进行的,作为回应我得到了这个(样本)值:

[255,216,255,224,0,16,74,70,73,70,0,1,1,1,0,96,0,96,0,0,255,219,0,67,0,4,2,3, 3,3,2,4,3,3,3,4,4,4,4,5,9,6,5,5,5,5,11,8,8,6,9,13,11, 13,13,13,11,12,12,14,16,20,17,14,15,19,15,12,12,18,24,18,19,21,22,23,23,23, 14,17,25,27,25,22,26,20,22,23,22,255,219,0,67,1,4,4,4,5,5,5,10,6,6,10,22, 15,12,15,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22, 22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22, 22,22,22,255,192,0,17,8,0,67,0,100,3,1,34,0,2,17,1,3,17,1,255,196,0,31,0,0,1,5, 1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,2,3,4,5,6,7,8,9]

注意:我修剪了值的内容,但这是我得到的数据。 现在这就是我获得价值的方式:

                foreach (JObject obj in arr)
                {
                    JArray jar = new JArray();                            
                    jar = obj["Thumbnail"] as JArray;

                }

如何将该值转换为byte []以便我可以在mvc视图中显示图像? 在此先感谢Laziale

3 个答案:

答案 0 :(得分:0)

只需使用String方法和LINQ来获取字节数组:

byte[] bytes = response.Trim('[', ']')
              .Split(',')
              .Select(x => byte.Parse(x))
              .ToArray();

答案 1 :(得分:0)

byte[] bytes = responce.Split(',').Select(s => Convert.ToByte(s, 16)).ToArray();

答案 2 :(得分:0)

byte[] byteItems = myJArray.Split(',').Select(z => byte.Parse(z)).ToArray();


If you dun't need to split then

byte[] byteItems = myJArray.Select(z => byte.Parse(z)).ToArray();