Introduction:
Here we will learn how to solve the problem of 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. Generally we will get this error
whenever our deserialized JSON
object returning list of object items but we are trying to hold only single
item.
Description:
In previous articles I explained asp.net json serialization and deserialization in c#, vb.net,
asp.net set custom error page inn web.config, unrecognized escape sequence in file path in c#, vb.net, cannot convert string to type double is not valid in vb.net,
jQuery show error message in ajax call response with example,
convert json string to json object with example and many
more articles related to in JSON,
asp.net,
mvc, c#,
vb.net.
Now I will explain how to solve the problem 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.
We are getting following error in our application
whenever we are try to deserialize the JSON
string in asp.net by using newtonsoft json.
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 Ã
select Manage Nuget Packages Ã
Go to Browse Tab Ã
Search for Newtonsoft Ã
From the list select Newtonsoft.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 of Serializing Data
Following is the result of DeSerializing the JSON Data.
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
|
|||
0 comments :
Note: Only a member of this blog may post a comment.