读书人

C# 读取Json内的数据,中文乱码,怎么解

发布时间: 2013-09-12 22:07:00 作者: rapoo

C# 读取Json内的数据,中文乱码,如何解决?
我把代码贴出来,大家看看!
这是网上找的一段代码


   private string getJson()
{
string url="http://218.228.332.114:8089/order/xxxxxx?prodid=330006";
string addressBookStr1 = null;//存放整个的字符串
HttpWebRequest addressRequest;//
HttpWebResponse addressResponse;//请求回应
try
{

addressRequest = (HttpWebRequest)WebRequest.Create(url);
addressRequest.Method = "GET";
addressResponse = (HttpWebResponse)addressRequest.GetResponse();
string t = addressResponse.ContentType;
string c = addressResponse.ContentEncoding;
Stream addressStream = addressResponse.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader readStream = new StreamReader(addressStream, encode);
Char[] read = new Char[256];
int count = readStream.Read(read, 0, 256);
String str = "";
while (count > 0)
{
// Dumps the 256 characters on a string and displays the string to the console.


str = new String(read, 0, count);
addressBookStr1 += str;
count = readStream.Read(read, 0, 256);
}
readStream.Close();
addressStream.Close();
addressResponse.Close();
}
catch
{

return null;
}
return addressBookStr1;
}



结果:

{"message": {"PRODNAME": "\u7cbe\u8c1b\u679c\u852c\u539f\u6c41\u673a", "PRESENTSCODE": "ZP330006", "BENEFIT": 100, "PRESENTNAME": "\u91d1\u5f6a\u9a6c\u9676\u74f7\u5200", "UNITPRICE": 1398.0, "PRODID": "330006", "POINT": null, "PRESENTNUM": 1, "PRESENTID": "ZP330006", "GETPOINT": 1398.0, "PRODTYPE": "0", "USEPOINT": "", "SCODE": "JDGSYZJ", "SPRICE": null, "SPEC": [{"SPEC3": null, "SPEC2": "JD--12", "STORENUM": 103, "SPEC1": "\u767d\u8272"}]}}


中文乱码,如何解决?
[解决办法]
Encoding encode = System.Text.Encoding.GetEncoding("Unicode");

试试,乱码一般都是编码问题造成的.

读书人网 >Web Service

热点推荐