TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Karthik K
1k
738
192k
How to assign Json array into model in asp.net ?
Dec 14 2020 8:22 PM
Hi All,
I having issue while i consume api into my application. when I try to assign it to model .its give me the error. i will show details about it .
string JsonProductResult = ObjApi.Api_ProductListByCategoryId(BarerTokenVal, Branch, T1);
var SObject = JsonConvert.DeserializeObject(JsonProductResult);
IList<Root> myDeserializedClass = JsonConvert.DeserializeObject<IList<Root>>(JsonProductResult);
JsonProductResult : (it return the following json array result )
"[\r\n {\r\n \"InventoryID\": 2,\r\n \"BarCode\": \"001\",\r\n \"InventoryName\": \"?????(??)\",\r\n \"Description\": \"Shun De Double Layered Pudding (Original Taste)\",\r\n \"Notes\": \"\",\r\n \"CategoryID\": 2,\r\n \"CategoryName\": \"?????\",\r\n \"Price\": 3.6000,\r\n \"ImagePath\": \"WebImage/cham1/001.jpg\"\r\n },\r\n {\r\n \"InventoryID\": 3,\r\n \"BarCode\": \"002\",\r\n \"InventoryName\": \"?????\",\r\n \"Description\": \"Chrysanthemum Double Layered Milk Pudding\",\r\n \"Notes\": \"\",\r\n \"CategoryID\": 2,\r\n \"CategoryName\": \"?????\",\r\n \"Price\": 3.8000,\r\n \"ImagePath\": \"WebImage/cham1/002.jpg\"\r\n },\r\n {\r\n \"InventoryID\": 4,\r\n \"BarCode\": \"003\",\r\n \"InventoryName\": \"???+??\",\r\n \"Description\": \"Double Layered Milk Pudding + Red Bean\",\r\n \"Notes\": \"\",\r\n \"CategoryID\": 2,\r\n \"CategoryName\": \"?????\",\r\n \"Price\": 3.8000,\r\n \"ImagePath\": \"WebImage/cham1/003.jpg\"\r\n },\r\n {\r\n \"InventoryID\": 5,\r\n \"BarCode\": \"004\",\r\n \"InventoryName\": \"???+??\",\r\n \"Description\": \"Double Layered Milk Pudding + Coconut\",\r\n \"Notes\": \"\",\r\n \"CategoryID\": 2,\r\n \"CategoryName\": \"?????\",\r\n \"Price\": 3.8000,\r\n \"ImagePath\": \"WebImage/cham1/004.jpg\"\r\n },\r\n {\r\n \"InventoryID\": 6,\r\n \"BarCode\": \"005\",\r\n \"InventoryName\": \"???+??\",\r\n \"Description\": \"Double Layered Milk Pudding + Lotus Seed\",\r\n \"Notes\": \"\",\r\n \"CategoryID\": 2,\r\n \"CategoryName\": \"?????\",\r\n \"Price\": 3.8000,\r\n \"ImagePath\": \"WebImage/cham1/005.jpg\"\r\n },\r\n {\r\n \"InventoryID\": 7,\r\n \"BarCode\": \"006\",\r\n \"InventoryName\": \"???+????\",\r\n \"Description\": \"Double Layered Milk Pudding + Red Bean Lotus Seed\",\r\n \"Notes\": \"\",\r\n \"CategoryID\": 2,\r\n \"CategoryName\": \"?????\",\r\n \"Price\": 4.0000,\r\n \"ImagePath\": \"WebImage/cham1/006.jpg\"\r\n },\r\n {\r\n \"InventoryID\": 8,\r\n \"BarCode\": \"007\",\r\n \"InventoryName\": \"???+??\",\r\n \"Description\": \"Double Layered Milk Pudding + Mango\",\r\n \"Notes\": \"\",\r\n \"CategoryID\": 2,\r\n \"CategoryName\": \"?????\",\r\n \"Price\": 4.5000,\r\n \"ImagePath\": \"WebImage/cham1/007.jpg\"\r\n },\r\n {\r\n \"InventoryID\": 9,\r\n \"BarCode\": \"008\",\r\n \"InventoryName\": \"???+???\",\r\n \"Description\": \"Double Layered Milk Pudding + Honeydew\",\r\n \"Notes\": \"\",\r\n \"CategoryID\": 2,\r\n \"CategoryName\": \"?????\",\r\n \"Price\": 4.5000,\r\n \"ImagePath\": \"WebImage/cham1/008.jpg\"\r\n },\r\n {\r\n \"InventoryID\": 10,\r\n \"BarCode\": \"009\",\r\n \"InventoryName\": \"???+??\",\r\n \"Description\": \"Double Layered Milk Pudding + Almond\",\r\n \"Notes\": \"\",\r\n \"CategoryID\": 2,\r\n \"CategoryName\": \"?????\",\r\n \"Price\": 4.8000,\r\n \"ImagePath\": null\r\n },\r\n {\r\n \"InventoryID\": 11,\r\n \"BarCode\": \"010\",\r\n \"InventoryName\": \"???+??\",\r\n \"Description\": \"Double Layered Milk Pudding + Walnut\",\r\n \"Notes\": \"\",\r\n \"CategoryID\": 2,\r\n \"CategoryName\": \"?????\",\r\n \"Price\": 4.9000,\r\n \"ImagePath\": null\r\n }\r\n]"
Sobject:
[
{
"InventoryID": 2,
"BarCode": "001",
"InventoryName": "?????(??)",
"Description": "Shun De Double Layered Pudding (Original Taste)",
"Notes": "",
"CategoryID": 2,
"CategoryName": "?????",
"Price": 3.6000,
"ImagePath": "WebImage/cham1/001.jpg"
},
{
"InventoryID": 3,
"BarCode": "002",
"InventoryName": "?????",
"Description": "Chrysanthemum Double Layered Milk Pudding",
"Notes": "",
"CategoryID": 2,
"CategoryName": "?????",
"Price": 3.8000,
"ImagePath": "WebImage/cham1/002.jpg"
},
{
"InventoryID": 4,
"BarCode": "003",
"InventoryName": "???+??",
"Description": "Double Layered Milk Pudding + Red Bean",
"Notes": "",
"CategoryID": 2,
"CategoryName": "?????",
"Price": 3.8000,
"ImagePath": "WebImage/cham1/003.jpg"
},
{
"InventoryID": 5,
"BarCode": "004",
"InventoryName": "???+??",
"Description": "Double Layered Milk Pudding + Coconut",
"Notes": "",
"CategoryID": 2,
"CategoryName": "?????",
"Price": 3.8000,
"ImagePath": "WebImage/cham1/004.jpg"
},
{
"InventoryID": 6,
"BarCode": "005",
"InventoryName": "???+??",
"Description": "Double Layered Milk Pudding + Lotus Seed",
"Notes": "",
"CategoryID": 2,
"CategoryName": "?????",
"Price": 3.8000,
"ImagePath": "WebImage/cham1/005.jpg"
},
{
"InventoryID": 7,
"BarCode": "006",
"InventoryName": "???+????",
"Description": "Double Layered Milk Pudding + Red Bean Lotus Seed",
"Notes": "",
"CategoryID": 2,
"CategoryName": "?????",
"Price": 4.0000,
"ImagePath": "WebImage/cham1/006.jpg"
},
{
"InventoryID": 8,
"BarCode": "007",
"InventoryName": "???+??",
"Description": "Double Layered Milk Pudding + Mango",
"Notes": "",
"CategoryID": 2,
"CategoryName": "?????",
"Price": 4.5000,
"ImagePath": "WebImage/cham1/007.jpg"
},
{
"InventoryID": 9,
"BarCode": "008",
"InventoryName": "???+???",
"Description": "Double Layered Milk Pudding + Honeydew",
"Notes": "",
"CategoryID": 2,
"CategoryName": "?????",
"Price": 4.5000,
"ImagePath": "WebImage/cham1/008.jpg"
},
{
"InventoryID": 10,
"BarCode": "009",
"InventoryName": "???+??",
"Description": "Double Layered Milk Pudding + Almond",
"Notes": "",
"CategoryID": 2,
"CategoryName": "?????",
"Price": 4.8000,
"ImagePath": null
},
{
"InventoryID": 11,
"BarCode": "010",
"InventoryName": "???+??",
"Description": "Double Layered Milk Pudding + Walnut",
"Notes": "",
"CategoryID": 2,
"CategoryName": "?????",
"Price": 4.9000,
"ImagePath": null
}
]
when i reach this following line , i getting converting errror .
IList<Root> myDeserializedClass = JsonConvert.DeserializeObject<IList<Root>>(JsonProductResult);
Name Value Type
InnerException {"Could not cast or convert from System.String to System.Collections.Generic.IList`1[Sun.Model.Root]."} System.Exception {System.ArgumentException}
this is my Model :
public
class
ProductList
{
public
int
InventoryID {
get
;
set
; }
public
string
BarCode {
get
;
set
; }
public
string
InventoryName {
get
;
set
; }
public
string
Description {
get
;
set
; }
public
string
Notes {
get
;
set
; }
public
int
CategoryID {
get
;
set
; }
public
string
CategoryName {
get
;
set
; }
public
decimal
Price {
get
;
set
; }
public
string
ImagePath {
get
;
set
; }
}
public
class
Root
{
public
IList<ProductList> ProductList {
get
;
set
; }
}
If anyone knows give me with a suitable solution for this .
Thanks in advace
Karthik . K
Reply
Answers (
2
)
Skype control using c#
using default value with same method name