Json.NET (Newtonsoft.Json) の基本的な使い方 | @Subaru
JSON.NETでシリアライズした文字列のJSON ValueにURLエンコードを掛けたいので、以下のようにしました。
回帰的になってませんが・・・
private class Test { public DateTime DT { get; set; } public int IT { get; set; } public string ST { get; set; } } Test a = new Test { DT = DateTime.Now, IT = 11431413, ST = "as$#121da+asa" }; Test b = new Test { DT = DateTime.Now, IT = 11431413, ST = "asda!41fsf,asa" }; List<Test> tests = new List<Test>{a,b}; var jsonStr = JsonConvert.SerializeObject(a); var arrayJsonStr = JsonConvert.SerializeObject(tests); JObject aaa = JObject.Parse(jsonStr); JArray aaaList = JArray.Parse(arrayJsonStr); foreach (KeyValuePair<string, JToken> keyValuePair in aaa) { aaa[keyValuePair.Key] = HttpUtility.UrlEncode((string) aaa[keyValuePair.Key]); } foreach (JObject token in aaaList) { foreach (KeyValuePair<string, JToken> keyValuePair in token) { token[keyValuePair.Key] = HttpUtility.UrlEncode((string)token[keyValuePair.Key]); } }
Thanks!