1

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);
5
  • try var items = JsonConvert.DeserializeObject<List<FoodItem>>(strProductsInCart->orders); Commented May 10, 2021 at 18:19
  • @user10099 OP does not have a list of objects they have object with list inside Commented May 10, 2021 at 18:20
  • 1
    You don't have an array of food objects you have an object with an array of food objects named orders. Commented May 10, 2021 at 18:21
  • @user10099 I have tried but its giving me error near strProductsInCart->orders) Commented May 10, 2021 at 18:21
  • @neem the two strings are identical. The top one is the escaped (and unindented) form of the other. The problem is that the classes don't match the JSON string Commented May 10, 2021 at 18:26

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@neem - I am wondering what are you going to do with the object of private values. You can't use them.
1

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;}
}

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.