0

I am trying to add data to my listview using json data returned by an api, I have a class with the structure of the list, however when i try and add the data in using a for loop its fails and doesn't add it in, it tries to run the loop once then exits the loop. On expecting the list it has a count of 0.

ListAvivity.cs

dynamic mFarms = JsonConvert.DeserializeObject(content);

List<MyList.Data> mItems = new List<FarmsList.Data>();

foreach(var items in mFarms.data)
{
  mItems.Add(new MyList.Data() {FarmID = items["FarmID"].ToString(), FarmerSurname = items["FarmerSurname"].ToString(), 
                FarmerForename = items["FarmerForename"].ToString(), FarmName = items["FarmName"].ToString()});
}

MyList.cs

public class MyList
{
    public class Data
    {
        public int FarmID { get; set; }
        public string FarmName { get; set; }
        public string FarmerSurname { get; set; }
        public string FarmerForename { get; set; }
    }

    public class RootObject
    {
        public int code { get; set; }
        public string status { get; set; }
        public string message { get; set; }
        public Data data { get; set; }
    }

}

Json Data

{
"code": 201,
"status": "Success",
"message": "Object found",
"data": [
    {
        "FarmID": 1,
        "FarmName": "Test1",
        "FarmerSurname": "Test",
        "FarmerForename": "Test"
    },
    {
        "FarmID": 2,
        "FarmName": "Test2",
        "FarmerSurname": "Test",
        "FarmerForename": "Test"
    }
  ]
 }
3
  • its fails and doesn't add it in. - So what does happen? Do you get an error? What is it? Commented Dec 15, 2017 at 17:08
  • nothing it tries once and then skips it Commented Dec 15, 2017 at 17:10
  • The example you've shown works fine for me, it loops twice as expected and reads the data correctly. Commented Dec 15, 2017 at 17:16

1 Answer 1

1

The program didn't enter the loop because you couldn't deserialize the json object. I suggest you to deserialize it as a known type instead of dynamic.

Create json classes;

public class Datum
{
    public int FarmID { get; set; }
    public string FarmName { get; set; }
    public string FarmerSurname { get; set; }
    public string FarmerForename { get; set; }
}

public class Content
{
    public int code { get; set; }
    public string status { get; set; }
    public string message { get; set; }
    public List<Datum> data { get; set; }
}

Then deserialize it;

var mFarms = JsonConvert.DeserializeObject<Content>(content);
List<MyList.Data> mItems = new List<FarmsList.Data>();

foreach(var items in mFarms.data)
{
  mItems.Add(new MyList.Data() {FarmID = items["FarmID"].ToString(), FarmerSurname = items["FarmerSurname"].ToString(), 
                FarmerForename = items["FarmerForename"].ToString(), FarmName = items["FarmName"].ToString()});
}

Also, to print mItems, build a loop;

foreach (var mitem in mItems)
{
    Console.WriteLine(mitem.FarmID);
    Console.WriteLine(mitem.FarmerSurname);
    //etc
}
Sign up to request clarification or add additional context in comments.

9 Comments

The provided JSON absolutely deserializes into a dynamic type and the loop works fine.
Yes, I tried it there is no problem with dynamic. But as I said it could be better to use a known type.
Yes, that is ideal, but it doesn't answer the question as the issue wasn't to do with whether OP deserialized into dynamic or a class anyway.
the app crashes when it tries to add the item into the list
What is the error ? Would you provide it in detail ?
|

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.