我使用JSON來獲取System.Net.WebResponse,然後通過調用StringBuilder.ToString()方法獲取響應結果之前將響應讀入到StringBuilder中。我無法使用Newtonsoft.Json.Linq.JObject.Parse(repsonse)解析這樣的respsone;stringbuilder不能跳過大括號
我的問題是,ToString()方法刪除我的'{'和'}'字符,因爲他們得到了轉義,除非與另一個'{'匹配。即使我做了StringBuilder.Replace(「{」,「{{」),它不起作用,因爲最後的括號逃脫第一支架 - >示例如下
我對得到響應代碼:
public static string GetResponseFromRequest(string url){
System.Net.WebRequest req = System.Net.WebRequest.Create(url);
System.Net.WebResponse res = req.GetResponse();
System.Text.StringBuilder sb = new System.Text.StringBuilder();
byte[] read = new byte[256];
int count = res.GetResponseStream().Read(read, 0, 256);
while (count > 0)
{
sb.Append(System.Text.ASCIIEncoding.ASCII.GetString(read));
count = res.GetResponseStream().Read(read, 0, 256);
}
res.GetResponseStream().Close();
res.Close();
return sb.ToString();
}
這裏是什麼我的反應看起來像一個示例:
{{
"id" : "myID",
"Name" : "MyDisplayName",
"description" : "A, MyDescription",
}"hasOverview" : true,
"hasDescription" : true,
}
後StringBuilder.ToString被稱爲它看起來像這樣:
"{
"id" : "myID",
"Name" : "MyDisplayName",
"description" : "A, MyDescription",
}"hasOverview" : true,
"hasDescription" : true,
"
如果我叫StringBuilder.Replace( 「{」, 「{{」)和StringBuilder.Replace( 「}」, 「}}」)我得到:
"{{
"id" : "myID",
"Name" : "MyDisplayName",
"description" : "A, MyDescription",
}}"hasOverview" : true,
"hasDescription" : true,
"
我需要一種方法來告訴的ToString( )返回字符串構建器所保存的字面表示,因此它不會考慮特殊字符。如果可能的話,特別是「{」和「}」字符。
StringBuilder的沒有按」不要修改角色。 – 2012-08-01 04:21:01
@JohnSaunders它確實如果你使用AppendFormat方法(替換格式化佔位符,它看起來像OP正在運行)。但我必須假設OP沒有這樣做。 – 2012-08-01 04:22:39
StringBuilder不會轉義括號。你如何知道在調用'StringBuilder.ToString'之前的響應是什麼樣的?你確定你沒有在你的例子中拿出代碼的相關部分嗎? – 2012-08-01 04:28:17