Karthik K

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 :
  1. public class ProductList  
  2. {  
  3. public int InventoryID { getset; }  
  4. public string BarCode { getset; }  
  5. public string InventoryName { getset; }  
  6. public string Description { getset; }  
  7. public string Notes { getset; }  
  8. public int CategoryID { getset; }  
  9. public string CategoryName { getset; }  
  10. public decimal Price { getset; }  
  11. public string ImagePath { getset; }  
  12. }  
  13. public class Root  
  14. {  
  15. public IList<ProductList> ProductList { getset; }  
  16. }  
If anyone knows give me with a suitable solution for this .
 
Thanks in advace
Karthik . K

Answers (2)