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. When I am parsing I am getting this error: Cannot deserialize the current JSON object (e.g. {“name”:“value”}) into type '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; } }
-
Answer:
Try to change hidden field value from HiddenField1.Value = "{\"AlertDataList\":[{...}]}" to HiddenField1.Value = "[{\"AlertDataList\":[{...}]}]"
Himanshu Bisht at Stack Overflow Visit the source
Other answers
You're deserializing to a List<alertMain>, yet your JSON contains simply one alertMain. Try: alertMain json = JsonConvert.DeserializeObject<alertMain>((HiddenField1.Value).ToString());
mattytommo
Answer from my comment. You are trying to convert List<alertMain>. alertMain already contains a list. Try using just alertMain since it is already containing the list of alerts.
Nikolay Kostov
You need to cast your json into List<IDictionary<string,string>> this will should be work. 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; } }
Mirza Danish Baig
Related Q & A:
- How can I convert Matlab code to c#?Best solution by Stack Overflow
- How Can I use .net dll in C program?Best solution by Stack Overflow
- How can I convert a string number to a number in Perl?Best solution by Stack Overflow
- How can I parse a complex XML with PHP and CDATA?Best solution by Stack Overflow
- How can I get my Tecumseh engine to get more power and speed?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.