0

I'm a bit new to c#, and I am trying to add all the records I return back from my web service and add them to their designated list. My JSON looks something like this:

{
  
 "errMsg":"",
   "date":"2020-10-21 10:20:28",
   "Category":
   [{"iCatID":"1","sDescription":"All},{"iCatID":"2","sDescription":"All Vegetables"},....],
 "Product":
   [{"iProductID":"10","sDescription":"Apples},   {"iProductID":"11","sDescription":"All Vegetables"},....]
}

Both Products and Categories are returning several 100 or 1000 products. SO far I have this to try and iterate through them and add them to their own list to then add both lists to Two local tables I have in my app called Product and Category.

I am clearly doing something wrong because my current code is not working. private List _product = new List(); private List _category = new List();

public async void CreateLocalTables()    
{

try
          {
            _client.GetAsync("https://app.spcty.com/app/dbExport.php");
                var content = await response.Content.ReadAsStringAsync();
                dynamic data = JsonConvert.DeserializeObject(content);

             foreach (var obj in data.Product)
              {
                  Console.WriteLine(obj);
                  //does not work
                  _product.Add(obj)
              }
}

My error is as follows Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: The best overloaded method match for 'System.Collections.Generic.List<Project.Models.Category>.Add(Project.Models.Category)' has some invalid arguments

I checked out my Category Class and I think I have everything I need.

public class Category
    {

        [PrimaryKey]
        public int iCatID { get; set; }
        public string sDescription { get; set; }
}

I don't get what it says invalid arguments. I get this for both category and product. Any recommendations? EDIT: I have also tried it this using the RootObject, I just didn't know how to access the product and category lists from there:

public class RootObject
{
    public  List<Category> CategoyItems { get; set; }

    public List<Product> ProductItems { get; set; }
    public string errMsg { get; set; }
    public string date { get; set; }            
}

I added two more things to both the JSON and RootObject.

4
  • Hi, could you show the example content data of response.Content.ReadAsStringAsync()? I will check the structure of data . Commented Oct 21, 2020 at 6:29
  • it's very long, but yeah let me see if I can get you that Commented Oct 21, 2020 at 16:33
  • @JuniorJiang-MSFT I added more of my JSON and I also added things to it, hopefully that gives you a better description. Commented Oct 21, 2020 at 17:30
  • Okey, thanks for updating. If you have solved this, remember to mark the answer when you have time. It will be helpful for other people to know the solution. Commented Oct 22, 2020 at 3:06

1 Answer 1

1

you need to deserialize your data into a concrete type

var data = JsonConvert.DeserializeObject<RootObject>(content);

foreach(var obj in data.ProductItems)
{
  _product.Add(obj);
}
Sign up to request clarification or add additional context in comments.

5 Comments

but my JSON data includes Category as well. I am pulling multiple arrays at once. One is Product and the other one is Category, that is why I had tried using dynamic.
okay so oddly enough now I am getting this error System.NullReferenceException: Object reference not set to an instance of an object in the line ''foreach(var obj in data.ProductItems)''. Do you know why that may be?
Because ProductItems is null. There is nothing with that name in your JSON so unless you have a mapping attribute it won’t deserialize correctly
ah I see. Okay I fixed that. I think I am on the right track now because I am getting this error now Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[SPRecipeApp2020.Models.Category_Type]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. I've dealt with that error before so I will dive into it
thanks again for you help. I had some issues in my JSON. But this worked

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.