Cole DeLavergne
Cole DeLavergne

Reputation: 13

C# API Cant get dynamic value to populate list

This program gets information from a database using API. The JSON file comes in and populates content. The content then populates the Dynamic "holder", however when I assign holder to "list" the data does not populate the list. I don't understand what I am doing wrong with this. Any help would be appreciated.

public class Rootobject
        {
        //Employer and Carrier information 
        public string Employer { get; set; }
        public string Phone { get; set; }
        public string InsUnder { get; set; }
        public string Carrier { get; set; }
        public string CarrierPh { get; set; }
        public string Group { get; set; }
        public string MailTo { get; set; }
        public string MailTo2 { get; set; }  //place holder
        public string MailTo3 { get; set; }
        public string EClaims { get; set; }
        public string FAXClaims { get; set; }
        public string DMOOption { get; set; }

    public class iapVm
    {
        public List<Rootobject> data { get; set; }
    }

    public static iapVm GetList(string iapNumber)
    {

        //Response object
        iapVm list = new iapVm();

        List<Rootobject> lstRootobject = new List<Rootobject>();

        //      //Create client object for request
        var client = new RestClient("http://myurls/json/"); /// call url

        var request = new RestRequest(Method.GET);

        //      //Add parameter
        request.AddParameter("Number", Number);

        //      //Create response object
        IRestResponse response = client.Execute(request);

        var content = response.Content;

        //      //Convert string to json

        dynamic holder = Newtonsoft.Json.JsonConvert.DeserializeObject(content);



        holder = list;

        list.data = lstRootobject;

        return list; 
}

Upvotes: 0

Views: 1030

Answers (1)

Joe_DM
Joe_DM

Reputation: 1003

I've done a quick test using the sample Json data you provided and it seems to work okay by deserializing the response content directly into a List<RootObject>

Heres what my test looks like: (I cut out all the web call stuff since I can't test it and we know you are getting the content back okay.)

 public class Rootobject
 {
     //Employer and Carrier information 
     public string Employer { get; set; }
     public string Phone { get; set; }
     public string InsUnder { get; set; }
     public string Carrier { get; set; }
     public string CarrierPh { get; set; }
     public string Group { get; set; }
     public string MailTo { get; set; }
     public string MailTo2 { get; set; } //place holder
     public string MailTo3 { get; set; }
     public string EClaims { get; set; }
     public string FAXClaims { get; set; }
     public string DMOOption { get; set; }
 }

 public class iapVm
 {
     public List<Rootobject> data { get; set; }
 }

 public class Class1
 {
     public iapVm TestDeserializingValidResponseContent()
     {
         //Response object
         iapVm list = new iapVm();
         string content = "[{\"_id\":\"asdf\",\"Employer\":\"1 800 Foo & Bar (Schedule Plan)\",\"EmpNumberXXX\":\"(333)-111-2222 : (800)-234-2344\",\"InsUnder\":\"asdf asdf\",\"DMOOption\":\"No\",\"Medical\":\"asdf (800)-234-2344 Group#:23443\",\"DateXXX\":\"May\",\"Carrier\":\"Cigna\",\"‌​CarrierPh\":\"(222)-‌​234-234234\",\"FAXClai‌​ms\":\"No. Will Not Accept\",\"Plan\":\"Self-Funded\",\"Group\":\"Cigna (800)-234-2344 Group#:23443\",\"GroupNum\":\"2343\",\"EClaims\":\"Yes\"}]";
         List<Rootobject> lstRootobject = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Rootobject>>(content);
         list.data = lstRootobject;
         return list;
     }
 }

Upvotes: 1

Related Questions