在此處提供了一些解決方案之後,我選擇了使用NewtonSoft。但我無法將JSON轉換爲類對象。我認爲,json(或字符串)以不正確的格式傳遞給方法。如何將JSON類似字符串轉換爲C#對象
類:
public class EmailAPIResult
{
public string Status { get; set; }
}
方法:
//....code
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
string result = streamReader.ReadToEnd();
//when hovered on "result", the value is "\"{\\\"Status\\\":\\\"Success\\\"}\""
//For Text Visualizer, the value is "{\"Status\":\"Success\"}"
//And for JSON Visualizer, the value is [JSON]:"{"Status":"Success"}"
EmailAPIResult earesult = JsonConvert.DeserializeObject<EmailAPIResult>(result);
//ERROR : Error converting value "{"Status":"Success"}" to type 'EmailAPIResult'.
}
下面的代碼工作:
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
string good = "{\"Status\":\"Success\"}";
//when hovered on "good", the value is "{\"Status\":\"Success\"}"
//For Text Visualizer, the value is {"Status":"Success"}
//And for JSON Visualizer, the value is
// [JSON]
// Status:"Success"
EmailAPIResult earesult = JsonConvert.DeserializeObject<EmailAPIResult>(good); //successful
}
我應該如何格式化 「結果」,讓我代碼有效。
http://json2csharp.com/ –
你甚至不寫如何你的JSON文件看起來像?!? –
也許你的'httpResponse'爲空 –