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

时间:2022-06-21 15:10:00

I tried the following code but am not able to parse json sting. When I am parsing I am getting this error:

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

Cannot deserialize the current JSON object (e.g. {“name”:“value”}) into type 'System.Collections.Generic.List`1

无法将当前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 个解决方案

#1


Try to change hidden field value from

尝试更改隐藏的字段值

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

to

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

#2


You're deserializing to a List<alertMain>, yet your JSON contains simply one alertMain.

您正在反序列化为List ,但您的JSON只包含一个alertMain。

Try:

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

#3


Answer from my comment.

回答我的评论。

You are trying to convert List<alertMain>. alertMain already contains a list.

您正在尝试转换List 。 alertMain已包含列表。

Try using just alertMain since it is already containing the list of alerts.

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

#4


You need to cast your json into List<IDictionary<string,string>> this will should be work.

你需要将你的json转换为List >这应该是有用的。

PFB code.

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

}

and you just create a one class like below:

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

public class RootObject
{
    public List<IDictionary<string, string>> AlertDataList { get; set; }
}

#1


Try to change hidden field value from

尝试更改隐藏的字段值

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

to

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

#2


You're deserializing to a List<alertMain>, yet your JSON contains simply one alertMain.

您正在反序列化为List ,但您的JSON只包含一个alertMain。

Try:

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

#3


Answer from my comment.

回答我的评论。

You are trying to convert List<alertMain>. alertMain already contains a list.

您正在尝试转换List 。 alertMain已包含列表。

Try using just alertMain since it is already containing the list of alerts.

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

#4


You need to cast your json into List<IDictionary<string,string>> this will should be work.

你需要将你的json转换为List >这应该是有用的。

PFB code.

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

}

and you just create a one class like below:

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

public class RootObject
{
    public List<IDictionary<string, string>> AlertDataList { get; set; }
}