Aesthetic123
Aesthetic123

Reputation: 35

Cannot convert JSON string to List of object

I have this JSON string that I want to convert to a List of objects in C# asp.net.

In VS I can see the json string appears like this:

"{\"orders\":[{\"ItemID\":\"6\",\"Quantity\":\"8\",\"CategoryID\":\"2\",\"Price\":\"0.5\",\"ItemName\":\"Focaccia\",\"ItemDesc\":\"Focaccia is a flat oven-baked Italian bread similar in style and texture to pizza; in some places, it is called pizza bianca. \",\"ItemImg\":\"Images/Focaccia.jpg\",\"InCart\":1}]}"

but in browser local storage, the json is well formatted and appears like this:

{
    "orders": [
        {
            "ItemID": "6",
            "Quantity": "8",
            "CategoryID": "2",
            "Price": "0.5",
            "ItemName": "Focaccia",
            "ItemDesc": "Focaccia is a flat oven-baked Italian bread similar in style and texture to pizza; in some places, it is called pizza bianca. ",
            "ItemImg": "Images/Focaccia.jpg",
            "InCart": 1
        }
    ]
}

I want to convert it to a FoodItem objects array list , below is the class code:

    public class FoodItem : Item
    {
        private string quantity;
        private string categoryID;
        private string price;
        private string itemName;
        private string itemDesc;
        private string itemImg;
        private string inCart;

        public FoodItem(string json)
        {
            JObject jObject = JObject.Parse(json);
            JToken jUser = jObject["orders"];
            quantity = (string)jUser["Quantity"];
            categoryID = (string)jUser["CategoryID"];
            price = (string)jUser["Price"];
            itemName =(string)jUser["itemDesc"];
            itemImg = (string)jUser["itemImg"];
            inCart = (string)jUser["inCart"];
        }
}

I have tried to use this below from previous threads but still having an error message.

Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[project.App_Code.FoodItem]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.

 var items = JsonConvert.DeserializeObject<List<FoodItem>>(strProductsInCart);

Upvotes: 1

Views: 4757

Answers (3)

Yong Shun
Yong Shun

Reputation: 51240

For JObject.Parse solution, you have to cast your orders as JArray type.

Next, convert it to List<FoodItem>.

string json = "{\"orders\":[{\"ItemID\":\"6\",\"Quantity\":\"8\",\"CategoryID\":\"2\",\"Price\":\"0.5\",\"ItemName\":\"Focaccia\",\"ItemDesc\":\"Focaccia is a flat oven-baked Italian bread similar in style and texture to pizza; in some places, it is called pizza bianca. \",\"ItemImg\":\"Images/Focaccia.jpg\",\"InCart\":1}]}";
    
JObject jObject = JObject.Parse(json);
JArray ordersArray = jObject["orders"] as JArray;
    
var orders = ordersArray.ToObject<List<FoodItem>>();

Sample program and print output

Upvotes: 0

Daniel Lorenz
Daniel Lorenz

Reputation: 4336

This is because you need a container class at the top that has this list of orders as a property inside of it:

public class MyData
{
     public List<FoodItem> Orders { get; set; }
{

var data = JsonConvert.DeserializeObject<MyData>(strProductsInCart);
var items = data.Orders;

Also, make sure you have public properties that match the name in the JSON. They can be Pascal case, too.

Upvotes: 5

Serge
Serge

Reputation: 43880

Try this:

class Program
{
    static void Main(string[] args)
    {
        string json = "{\"orders\":[{\"ItemID\":\"6\",\"Quantity\":\"8\",\"CategoryID\":\"2\",\"Price\":\"0.5\",\"ItemName\":\"Focaccia\",\"ItemDesc\":\"Focaccia is a flat oven-baked Italian bread similar in style and texture to pizza; in some places, it is called pizza bianca. \",\"ItemImg\":\"Images/Focaccia.jpg\",\"InCart\":1}]}";

        var orderList = JsonConvert.DeserializeObject<OrderList>(json);
       var orders=orderList.Orders;
        
    }
}
public class OrderList
{
    public List<FoodItem> Orders {get; set;}
}
public class FoodItem
{
    public string quantity  {get; set;}
    public string categoryID  {get; set;}
    public string price  {get; set;}
    public string itemName  {get; set;}
    public string itemDesc  {get; set;}
    public string itemImg  {get; set;}
    public string inCart  {get; set;}
}

Upvotes: 1

Related Questions