c#


Cannot deserialize the current JSON object into type 'System.Collections.Generic.List


I am trying to read my JSON result.
Here is my JsonResult
public class JsonResult
{
public string ResponseStatus;
public string Status;
public string Remarks;
public string ErrorCode;
public List<Data> data;
}
public class Data
{
public string Status;
public DateTime Date;
public string Number;
public string Amount;
public string Balance;
public string TranId;
public string OPTId;
public string RefId;
}
Here is my JSON result
{
"ResponseStatus":"1",
"Status":"Success",
"Remarks":"Your recharge has been Pending.",
"ErrorCode":"3",
"Data":
{
"Status":"Pending",
"Date":"2017-02-23T17:22:26.2001954+05:30",
"Number":"9915933511",
"Amount":10.0,
"Balance":137.714,
"TranId":"1126887",
"OPTId":"","RefId":""
}
}
Here is my code to read the result
var result = JsonConvert.DeserializeObject<List<JsonResult>>(retPKT);
But Getting Exception..
Cannot deserialize the current JSON object (e.g. {"name":"value"})
into type 'System.Collections.Generic.List`1[JsonResult+Data]' because
the type requires a JSON array (e.g. [1,2,3]) to deserialize
correctly. To fix this error either change the JSON to a JSON array
(e.g. [1,2,3]) or change the deserialized type so that it is a normal
.NET type (e.g. not a primitive type like integer, not a collection
type like an array or List) that can be deserialized from a JSON
object. JsonObjectAttribute can also be added to the type to force it
to deserialize from a JSON object. Path 'Data.Status', line 1,
position 118.
How can I fix this?
In your Json string, Data is an object, not an array. So your JsonResult class should look like this:
public class JsonResult
{
public string ResponseStatus;
public string Status;
public string Remarks;
public string ErrorCode;
public Data Data;
}
If you want to send list of data List<Data> then in JSON it should be in a form of array
Change the following JSON content
"Data": {"Status":"Pending",
"Date":"2017-02-23T17:22:26.2001954+05:30",
"Number":"9915933511",
"Amount":10.0,"Balance":137.714,
"TranId":"1126887","OPTId":"","RefId":""}
To
"Data": [{"Status":"Pending",
"Date":"2017-02-23T17:22:26.2001954+05:30",
"Number":"9915933511",
"Amount":10.0,"Balance":137.714,
"TranId":"1126887","OPTId":"","RefId":""}]
Data isn't a list, it's an object. Your models should look like this (Only JsonResult is modified):
public class Data
{
public string Status { get; set; }
public string Date { get; set; }
public string Number { get; set; }
public double Amount { get; set; }
public double Balance { get; set; }
public string TranId { get; set; }
public string OPTId { get; set; }
public string RefId { get; set; }
}
public class JsonResult
{
public string ResponseStatus { get; set; }
public string Status { get; set; }
public string Remarks { get; set; }
public string ErrorCode { get; set; }
public Data Data { get; set; }
}
Hope it helps!

Related Links

Use WebClient.DownloadString with https uri using MVC5
monitoring mysql database in c#
Selenium Firefox Driver problems
Sending a value from a class to a form
Convert string to generic T in Entity Framework and invoke methods [duplicate]
Crystal Reports keeps giving me: Could not load file or assembly
SQL DataSource Commands not working
Handling dervied types in a function accepting base type
Error when deserializing
How to get start & end time of session in asp.net c#
How do I align controls in FlowLayoutPanel properly
How to get unicode value used XmlReader class in C#
Visual Studio 13 doesn't build all changes
How to change color of line under WizardControl?
Executing an Oracle stored procedure within EntityFramework
Dynamics CRM Date filter is not working on Opportunity transaction type on Estimated Close Date

Categories

HOME
oauth-2.0
marie
qemu
popover
spotfire
hex
hiveql
schema
doctrine2
annyang
pyodbc
dcos
coding-style
spring-data-solr
osrm
xmlhttprequest
browsermob
repeater
lambda-calculus
background-image
internet-explorer-7
ringcentral
novacode-docx
conditional-formatting
connection-pooling
apdu
windowserror
setup-project
uibarbuttonitem
kundera
mobile-development
imdb
jcr
ports
bootcamp
android-geofence
pymunk
yelp
area
autodesk-data-management
datagridviewcombobox
taiga
smooth-streaming
gradlew
niagara-ax
lucee
riemann
enet
typewriter
liferay-ide
opticalflow
trusted-computing
timex
stripes
android-tabs
easy68k
mongodb-3.3
strtol
require-handlebars
falcon
aws-kms
jcalendar
cexception
wordpress-plugin
google-app-engine-php
matlab-engine
redquerybuilder
musicbrainz
glog
ultraedit
beagleboard
nexusdb
kss
register-allocation
formbuilder
nsnotificationcenter
system.net
rails-migrations
dateadd
sendy
iwork
class-library
objectdatasource
wpd
kademlia
servicemanager
gpl
qt5.1
algol
invalidoperationexception
teamsystem
expresso-store
dancer
mysql-connector
facebook-winjs-sdk
zipstream
subdirectories
uccapi
authkit
user-preferences
onresize
j++

Resources

Encrypt Message