Camilo Riviere
Camilo Riviere

Reputation: 51

Trouble parsing this json

Okay, so I have this response model below and I'm trying to retrieve a List of Daily. Below the response model and the receiving json. I added an additional snippet of code where I retrieve a list of agent objects, but I have not been able to retrieve a Daily List from the list of agent objects. I'm not sure what I'm doing wrong here and could really use some help. Thank you.

    {
    "accountId": "",
    "policiesInForce": [
    {
      "daily": [
    {
      "date": "Date",
      "pifCount": "",
      "noPopPifCount": "",
      "popPifCount": "",
      "cleanPopPifCount": ""
    }
  ]
}
],
"pathsToPartnership": [
{
  "pathsToPartnershipInd": "bool",
  "pathsToPartnershipLevel": ""
}
],
"overrides": [
{
  "startDate": "date",
  "endDate": "date",
  "overrideTier": "",
  "turnOffPlatinumFlag": "bool"
}
   ],
 "agents": [
  {
  "agentCode": "",
  "pathsToPartnership": [
    {
      "pathsToPartnershipInd": "bool",
      "pathsToPartnershipLevel": ""
    }
  ],
  "policiesInForce": [
    {
      "daily": [
        {
          "date": "Date",
          "pifCount": "",
          "noPopPifCount": "",
          "popPifCount": "",
          "cleanPopPifCount": ""
        }
      ]
    }
  ],
  "exceptions": [
    {
      "platinumInd": "bool",
      "newAgentTenureDate": "date",
      "maAgentTenureDate": "date",
      "residentStCd": "",
      "clPremiumAmt": ""
    }
  ],
  "overrides": [
    {
      "startDate": "date",
      "endDate": "date",
      "overrideTier": "",
      "turnOffPlatinumFlag": "bool"
    }
  ],
  "commissionLevel": [
    {
      "pifLevel": "",
      "effectiveDate": "Date",
      "endDate": "Date"
    }
  ]
  }
  ]

}

public class PoliciesInForce
{
    public List<Daily> Daily { get; set; }
}

public class Daily
{
    public string Date { get; set; }
    public int PifCnt { get; set; }
    public int NoPopPifCnt { get; set; }
    public int PopPifCnt { get; set; }
    public int CleanPopPifCount { get; set; }
}

public class PoliciesInForce2
{
    public List<Daily> Daily { get; set; }
}

public class CommissionLevels
{
    public List<object> CommissionLevel { get; set; }
}

public class Exceptions
{
    public string ExceptionId { get; set; }
    public string PlatinumInd { get; set; }
    public string NewAgentTenureDate { get; set; }
    public string MAAgentTenureDate { get; set; }
    public string ResidentStCd { get; set; }
    public object CLPremiumAmt { get; set; }
}

public class PathToPartnership
{
    public string LevelCode { get; set; }
    public string LevelName { get; set; }
    public string StartDate { get; set; }
    public object EndDate { get; set; }
}

public class Agent
{
    public string AgentCode { get; set; }
    public PoliciesInForce2 PoliciesInForce { get; set; }
    public CommissionLevels CommissionLevels { get; set; }
    public Exceptions Exceptions { get; set; }
    public PathToPartnership PathToPartnership { get; set; }
}

public class PathToPartnership2
{
    public string LevelCode { get; set; }
    public string LevelName { get; set; }
    public string StartDate { get; set; }
    public string EndDate { get; set; }
}

public class Account
{
    public int AccountId { get; set; }
    public string CommissionLevel { get; set; }
    public PoliciesInForce PoliciesInForce { get; set; }
    public List<Agent> Agents { get; set; }
    public PathToPartnership2 PathToPartnership { get; set; }
}

public class RootObject
{
    public Account Account { get; set; }
}

}

var agents = new List<Agent>();
                string response = message.Content.ReadAsStringAsync().Result;
                var account = JsonConvert.DeserializeObject<RootObject>(response);
                var list = new List<Daily>();

                foreach (Agent agent in account.Account.Agents)
                {
                    agents.Add(agent);
                }

                for (int i = 0; i < agents.Count; i++)
                {
                    list[i].Date = agents[i].PoliciesInForce.Daily[i].Date;
                    list[i].PifCnt = agents[i].PoliciesInForce.Daily[i].PifCnt;
                    list[i].NoPopPifCnt = agents[i].PoliciesInForce.Daily[i].NoPopPifCnt;
                    list[i].PopPifCnt = agents[i].PoliciesInForce.Daily[i].PopPifCnt;
                    list[i].CleanPopPifCount = agents[i].PoliciesInForce.Daily[i].CleanPopPifCount;
                }

Upvotes: 0

Views: 60

Answers (1)

Sach
Sach

Reputation: 10393

This creates an empty list.

var list = new List<Daily>();

Then you're trying to assign to an index in the list, when there are no items in the list.

for (int i = 0; i < agents.Count; i++)
{
    list[i].Date = agents[i].PoliciesInForce.Daily[i].Date;
    list[i].PifCnt = agents[i].PoliciesInForce.Daily[i].PifCnt;
    list[i].NoPopPifCnt = agents[i].PoliciesInForce.Daily[i].NoPopPifCnt;
    list[i].PopPifCnt = agents[i].PoliciesInForce.Daily[i].PopPifCnt;
    list[i].CleanPopPifCount = agents[i].PoliciesInForce.Daily[i].CleanPopPifCount;
}

That is, when you say list[i], it assumes that list[i] has an instance of Daily.

What you need to do is Add().

for (int i = 0; i < agents.Count; i++)
{
    list.Add(new Daily
    {
        Date = agents[i].PoliciesInForce.Daily[i].Date,
        PifCnt = agents[i].PoliciesInForce.Daily[i].PifCnt,
        NoPopPifCnt = agents[i].PoliciesInForce.Daily[i].NoPopPifCnt,
        PopPifCnt = agents[i].PoliciesInForce.Daily[i].PopPifCnt,
        CleanPopPifCount = agents[i].PoliciesInForce.Daily[i].CleanPopPifCount
    });
}

Upvotes: 1

Related Questions