C#如何从System.IO.StreamReader JSON格式转换为字符串

时间:2016-03-24 11:34:01

标签: c# tostring streamreader

  System.Net.WebRequest req = System.Net.WebRequest.Create(this.url_);
        req.Method = "POST";
        req.Timeout = 100000;
        req.ContentType = "application/json";
        byte[] sentData = Encoding.GetEncoding(1251).GetBytes(this.data_);
        req.ContentLength = sentData.Length;
        System.IO.Stream sendStream = req.GetRequestStream();
        sendStream.Write(sentData, 0, sentData.Length);
        sendStream.Close();
        System.Net.WebResponse res = req.GetResponse();
        System.IO.Stream ReceiveStream = res.GetResponseStream();
        System.IO.StreamReader sr = new System.IO.StreamReader(ReceiveStream, Encoding.UTF8);
        //Кодировка указывается в зависимости от кодировки ответа сервера
        Char[] read = new Char[256];
        string value = sr.ReadToEnd();
        int count = sr.Read(read, 0, 256);
        string Out = String.Empty;
        while (count > 0)
        {
            String str = new String(read, 0, count);
            string s = new string(read);
            Out += str;
            requestedData_ += s;
            count = sr.Read(read, 0, 256);
        }
        return Out;

我需要像

这样的JSON格式
{"object":{"1":"DEPO Name","244":"Lviv","245":"Lviv","246":"Lviv_2","247":"Lviv_2","248":"Lviv_2","249":"Lviv_2"},"errors":[],"fields":{}}

但它返回

"{\"object\":{\"1\":\"DEPO Name\",\"244\":\"Lviv\",\"245\":\"Lviv\",\"246\":\"Lviv_2\",\"247\":\"Lviv_2\",\"248\":\"Lviv_2\",\"249\":\"Lviv_2\"},\"errors\":[],\"fields\":{}}"

如何解决?

在这里你会看到 http://json2csharp.com/

我无法转换

PostTemplate value = new PostTemplate(this.url_ + url, data);
            string result = value.GetData();
            AddedDepoId returnId = JsonConvert.DeserializeObject<AddedDepoId>
(result);

我读过POST回答(是JSON格式)

1 个答案:

答案 0 :(得分:0)

我假设你在调试器中看到了这个值。

字符串正确,其值为escaped

使用Console.Writeline查看该值。