如何在c#中解析JSON字符串获取错误无法反序列化当前的JSON对象(例如{“name”:“value”})

时间:2015-05-07 08:10:21

标签: c# json

我尝试了以下代码,但无法解析json sting。什么时候 我正在解析我收到此错误:

  

无法将当前JSON对象(例如{“name”:“value”})反序列化为类型'System.Collections.Generic.List`1

   HiddenField1.Value = "{\"AlertDataList\":[{\"header\":\"YTD Noresp 04/01/2010 - 01/31/2011 Created On 10/02/2013\",\"type\":\"High      \",\"NoResponse\":\"0\",\"NoResponsePct\":\"      0.00%\",\"Response\":\"1\",\"ResponsePct\":\"    100.00%\",\"All\":\"1\",\"accountperiodid\":\"11306\",\"navigateUrl\":\"windowpane://Insights.exe/HCI.Insights.Windows.Forms.Converted.FormFinalert…&responseType=NoResp&accountPeriodId=11306&alertType=High&hasResponses=All\",\"navigateResponsesUrl\":\"windowpane://Insights.exe/HCI.Insights.Windows.Forms.Converted.FormFinalert…responseType=NoResp&accountPeriodId=11306&alertType=High&hasResponses=Resp\",\"navigateNoResponsesUrl\":\"windowpane://Insights.exe/HCI.Insights.Windows.Forms.Converted.FormFinalert…D&responseType=NoResp&accountPeriodId=11306&alertType=High&hasResponses=No Resp\"},{\"header\":\"YTD Noresp 04/01/2010 - 01/31/2011 Created On 10/02/2013\",\"type\":\"Medium\",\"NoResponse\":\"0\",\"NoResponsePct\":\"      0.00%\",\"Response\":\"1\",\"ResponsePct\":\"    100.00%\",\"All\":\"1\",\"accountperiodid\":\"11306\",\"navigateUrl\":\"windowpane://Insights.exe/HCI.Insights.Windows.Forms.Converted.FormFinalert…esponseType=NoResp&accountPeriodId=11306&alertType=Medium&hasResponses=All\",\"navigateResponsesUrl\":\"windowpane://Insights.exe/HCI.Insights.Windows.Forms.Converted.FormFinalert…sponseType=NoResp&accountPeriodId=11306&alertType=Medium&hasResponses=Resp\",\"navigateNoResponsesUrl\":\"windowpane://Insights.exe/HCI.Insights.Windows.Forms.Converted.FormFinalert…responseType=NoResp&accountPeriodId=11306&alertType=Medium&hasResponses=No Resp\"},{\"header\":\"YTD Noresp 04/01/2010 - 01/31/2011 Created On 10/02/2013\",\"type\":\"All\",\"NoResponse\":\"0\",\"NoResponsePct\":\"      0.00%\",\"Response\":\"2\",\"ResponsePct\":\"    100.00%\",\"All\":\"2\",\"accountperiodid\":\"11306\",\"navigateUrl\":\"windowpane://Insights.exe/HCI.Insights.Windows.Forms.Converted.FormFinalert…D&responseType=NoResp&accountPeriodId=11306&alertType=All&hasResponses=All\",\"navigateResponsesUrl\":\"windowpane://Insights.exe/HCI.Insights.Windows.Forms.Converted.FormFinalert…&responseType=NoResp&accountPeriodId=11306&alertType=All&hasResponses=Resp\",\"navigateNoResponsesUrl\":\"windowpane://Insights.exe/HCI.Insights.Windows.Forms.Converted.FormFinalert…TD&responseType=NoResp&accountPeriodId=11306&alertType=All&hasResponses=No Resp\"}]}"


    public void AlertTable()
    {           
        List<alertMain> json = JsonConvert.DeserializeObject <List<alertMain>>((HiddenField1.Value).ToString());          

    }

public class alert
{
    [JsonProperty("header")]
    public string header { get; set; }

    [JsonProperty("type")]
    public string type { get; set; }

    [JsonProperty("NoResponse")]
    public string NoResponse { get; set; }

    [JsonProperty("NoResponsePct")]
    public string NoResponsePct { get; set; }

    [JsonProperty("Response")]
    public string Response { get; set; }

    [JsonProperty("ResponsePct")]
    public string ResponsePct { get; set; }

    [JsonProperty("All")]
    public string All { get; set; }

    [JsonProperty("accountperiodid")]
    public string accountperiodid { get; set; }

    [JsonProperty("navigateUrl")]
    public string navigateUrl { get; set; }

    [JsonProperty("navigateResponsesUrl")]
    public string navigateResponsesUrl { get; set; }

    [JsonProperty("navigateNoResponsesUrl")]
    public string navigateNoResponsesUrl { get; set; }        
}

public class alertMain
{
    [JsonProperty("AlertDataList")]
    public List<alert> AlertDataList { get; set; }
}

4 个答案:

答案 0 :(得分:0)

尝试从

更改隐藏字段值
HiddenField1.Value = "{\"AlertDataList\":[{...}]}"

HiddenField1.Value = "[{\"AlertDataList\":[{...}]}]"

答案 1 :(得分:0)

您要反序列化为List<alertMain>,但您的JSON只包含一个alertMain

尝试:

alertMain json = JsonConvert.DeserializeObject<alertMain>((HiddenField1.Value).ToString());          

答案 2 :(得分:0)

从我的评论中回答。

您正在尝试转换List<alertMain>alertMain已包含一个列表。

请尝试仅使用alertMain,因为它已包含警报列表。

答案 3 :(得分:0)

你需要将你的json转换为modal这应该可行。

PFB代码。

List<IDictionary<string,string>>

你只需创建一个如下所示的类:

public void AlertTable()
{
            var json = JsonConvert.DeserializeObject<RootObject>((HiddenField1.Value).ToString());

}