简体   繁体   English

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

[英]How can i parse JSON string in c# Get error Cannot deserialize the current JSON object (e.g. {“name”:“value”})

I tried the following code but am not able to parse json sting. 我尝试了以下代码,但无法解析json sting。 When I am parsing I am getting this error: 当我解析时,我收到此错误:

Cannot deserialize the current JSON object (eg {“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; }
}

Try to change hidden field value from 尝试更改隐藏的字段值

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

to

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

You're deserializing to a List<alertMain> , yet your JSON contains simply one alertMain . 您正在反序列化为List<alertMain> ,但您的JSON只包含一个alertMain

Try: 尝试:

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

Answer from my comment. 回答我的评论。

You are trying to convert List<alertMain> . 您正在尝试转换List<alertMain> alertMain already contains a list. alertMain已包含列表。

Try using just alertMain since it is already containing the list of alerts. 尝试使用alertMain因为它已经包含警报列表。

You need to cast your json into List<IDictionary<string,string>> this will should be work. 你需要将你的json转换为List<IDictionary<string,string>>这应该是有用的。

PFB code. PFB代码。

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; }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C# 无法反序列化当前的 JSON object (eg {“name”:“value”}) - C# Cannot deserialize the current JSON object (e.g. {“name”:“value”}) 无法反序列化当前的 JSON object(例如 {“name”:“value”} - Cannot deserialize the current JSON object (e.g. {“name”:“value”} 无法反序列化当前JSON对象(例如{“ name”:“ value”}) - Cannot deserialize the current JSON object (e.g. {“name”:“value”}) 无法反序列化当前的 JSON 对象(例如 {“name”:“value”}) - Cannot deserialize the current JSON object (e.g. {“name”:“value”}) 无法将当前 JSON object(例如 {“name”:“value”})反序列化为类型需要 JSON 数组(例如 [1,2,3] 才能正确反序列化) - Cannot deserialize the current JSON object (e.g. {“name”:“value”}) into type requires a JSON array (e.g. [1,2,3]) to deserialize correctly 如何修复“无法将当前的 JSON 数组反序列化为“段落”类型,因为需要 JSON object(例如 {“name”:“值”) - How to fix 'Cannot deserialize the current JSON array into type 'passages' because requires a JSON object (e.g. {“name”:“value”}) to deserialize 无法将当前JSON数组(例如[1,2,3])反序列化为类型”,因为该类型需要JSON对象(例如{“ name”:“ value”}) - Cannot deserialize the current JSON array (e.g. [1,2,3]) into type '' because the type requires a JSON object (e.g. {“name”:“value”}) 将 json 反序列化以列出 object 抛出错误。 无法反序列化当前的 JSON object (eg {"name":"value"}) - Deserilizing the json to list of an object throwing error. Cannot deserialize the current JSON object (e.g. {"name":"value"}) Newtonsoft.Json.JsonSerializationException: '无法反序列化当前的 JSON object (例如 {"name":"value"}) - Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) 无法将当前 JSON 对象(例如 {&quot;name&quot;:&quot;value&quot;})反序列化为类型 - Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM