JSON を使用して System.Net.WebResponse を取得し、StringBuilder.ToString() メソッドを呼び出して応答の結果を取得する前に、応答を StringBuilder に読み込みます。Newtonsoft.Json.Linq.JObject.Parse(repsonse); を使用してそのようなレスソンを解析できません。
私の問題は、別の「{」と一致しない限りエスケープされるため、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() に指示する方法が必要です。可能であれば、特に「{」と「}」の文字のみ。