HI WELCOME TO Sirees

Cannot deserialize the current JSON object (e.g. {"name":"value"})

Leave a Comment


We are getting following error in our application whenever we are try to deserialize the JSON string in asp.net by using newtonsoft json.

cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[userdetails]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly
Actually this problem occurred whenever our JSON deserialization method returns more than one list item but we are trying to hold it as single item like as shown following.

C# Code


string strmsg = "[{\"userid\":1,\"username\":\"suresh\",\"location\":\"chennai\"},{\"userid\":2,\"username\":\"rohini\",\"location\":\"guntur\"}]";
userdetails user = JsonConvert.DeserializeObject<userdetails>(strmsg);

VB.NET Code


Dim strmsg As String = "[{""userid"":1,""username"":""suresh"",""location"":""chennai""},{""userid"":2,""username"":""rohini"",""location"":""guntur""}]"
Dim user As userdetails = JsonConvert.DeserializeObject(Of userdetails)(strmsg)

We need to change userdetails user parameter to var user like as shown following because our deserialization method returning more than one list item.

C# Code


string strmsg = "[{\"userid\":1,\"username\":\"suresh\",\"location\":\"chennai\"},{\"userid\":2,\"username\":\"rohini\",\"location\":\"guntur\"}]";
var user = JsonConvert.DeserializeObject<List<userdetails>>(strmsg);

VB.NET Code


Dim strmsg As String = "[{""userid"":1,""username"":""suresh"",""location"":""chennai""},{""userid"":2,""username"":""rohini"",""location"":""guntur""}]"
Dim user = JsonConvert.DeserializeObject(Of List(Of userdetails))(strmsg)

If you want complete example to implement serialization and deserialization for JSON data create new web application and open your Default.aspx page and write the code like as shown below.


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>JSON Serialization and Deserialization in Asp.Net</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Button ID="btnSerialize" runat="server" Text="Serialize" OnClick="btnSerialize_Click" />
<asp:Button ID="btnDeserialize" runat="server" Text="DeSerialize" OnClick="btnDeserialize_Click"/>
<div>
Serialized Data: <asp:Label ID="lblserial" runat="server"/>
</div>
<div>
DeSerialized Data: <asp:Label ID="lbldeserial" runat="server"/>
</div>
</form>
</body>
</html>

Now open code behind file and write the code like as shown below

C# Code


using System;
using System.Collections.Generic;
using Newtonsoft.Json;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void btnSerialize_Click(object sender, EventArgs e)
{
List<userdetails> details = new List<userdetails>();
userdetails user = new userdetails();
details.Add(new userdetails { userid = 1, username = "suresh", location = "chennai" });
details.Add(new userdetails { userid = 2, username = "rohini", location = "guntur" });
details.Add(new userdetails { userid = 3, username = "praveen", location = "bangalore" });
details.Add(new userdetails { userid = 4, username = "sateesh", location = "vizag" });
details.Add(new userdetails { userid = 5, username = "madhav", location = "nagpur" });
details.Add(new userdetails { userid = 6, username = "honey", location = "nagpur" });
string strserialize = JsonConvert.SerializeObject(details);
lblserial.Text = strserialize;
}

protected void btnDeserialize_Click(object sender, EventArgs e)
{
string strmsg = "[{\"userid\":1,\"username\":\"suresh\",\"location\":\"chennai\"},{\"userid\":2,\"username\":\"rohini\",\"location\":\"guntur\"}]";
var user = JsonConvert.DeserializeObject<List<userdetails>>(strmsg);
}
}
class userdetails
{
public int userid { get; set; }
public string username { get; set; }
public string location { get; set; }
}


VB.NET Code


Imports Newtonsoft.Json
Partial Class VBCode
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

End Sub
Protected Sub btnSerialize_Click(sender As Object, e As EventArgs)
Dim details As New List(Of userdetails)()
Dim user As New userdetails()
details.Add(New userdetails() With {
.userid = 1,
.username = "suresh",
.location = "chennai"
})
details.Add(New userdetails() With {
.userid = 2,
.username = "rohini",
.location = "guntur"
})
details.Add(New userdetails() With {
.userid = 3,
.username = "praveen",
.location = "bangalore"
})
details.Add(New userdetails() With {
.userid = 4,
.username = "sateesh",
.location = "vizag"
})
details.Add(New userdetails() With {
.userid = 5,
.username = "madhav",
.location = "nagpur"
})
details.Add(New userdetails() With {
.userid = 6,
.username = "honey",
.location = "nagpur"
})
Dim strserialize As String = JsonConvert.SerializeObject(details)
lblserial.Text = strserialize
End Sub
Protected Sub btnDeserialize_Click(sender As Object, e As EventArgs)
Dim strmsg As String = "[{""userid"":1,""username"":""suresh"",""location"":""chennai""},{""userid"":2,""username"":""rohini"",""location"":""guntur""}]"
Dim user = JsonConvert.DeserializeObject(Of List(Of userdetails))(strmsg)
End Sub
End Class

Public Class userdetails
Public Property userid() As Integer
Get
Return m_userid
End Get
Set
m_userid = Value
End Set
End Property
Private m_userid As Integer
Public Property username() As String
Get
Return m_username
End Get
Set
m_username = Value
End Set
End Property
Private m_username As String
Public Property location() As String
Get
Return m_location
End Get
Set
m_location = Value
End Set
End Property
Private m_location As String
End Class

If you observe above code we added namespace “Newtonsoft.JSON” this we can get by adding reference using Manage Nuget Packages. To add reference right click on your application à selectManage Nuget Packages à Go to Browse Tab à Search for Newtonsoft à From the list selectNewtonsoft.Json and install it. Once we install the component that will show like as shown following.

Demo

Now run the application to see the result that will be like as shown below. Following is the result ofSerializing Data

Following is the result of DeSerializing the JSON Data.

This is how we can fix the JSON deserialization error in our applications.

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.