【问题标题】:Trouble parsing this json无法解析此 json
【发布时间】:2017-08-18 20:00:53
【问题描述】:

好的,我在下面有这个响应模型,我正在尝试检索每日列表。下面是响应模型和接收 json。我添加了一个额外的 sn-p 代码,用于检索代理对象列表,但我无法从代理对象列表中检索每日列表。我不确定我在这里做错了什么,并且真的可以使用一些帮助。谢谢。

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

【问题讨论】:

  • 但我无法从代理对象列表中检索每日列表。为什么?当你尝试时会发生什么?错误?意外行为?您应该更清楚地解释您面临的问题。
  • 该列表未填充。你初始化它然后尝试分配一个索引?
  • 当 for 循环第二次迭代时,我收到一个空引用异常。
  • 我已经包含了 json 响应。

标签: c# json api parsing


【解决方案1】:

这将创建一个 列表。

var list = new List<Daily>();

然后,当列表中没有项目时,您尝试分配给列表中的索引。

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

也就是说,当你说list[i] 时,它假定list[i] 有一个Daily 的实例。

你需要做的是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
    });
}

【讨论】:

  • 一次成功添加到列表中,但在第二次迭代时失败。
  • 我猜这是因为你使用agents[i].PoliciesInForce.Daily[i] 的方式。也就是说,让我们进行循环的第二次迭代,此时i = 1。现在,您如何保证在agent[1].PoliciesInForce.Daily 列表中您有两个 项?这取决于你的 account 变量有什么数据,因此不能保证你会有两个对象。因此出现空引用异常。
  • 如果我可能会问,在这种情况下防止空引用异常的正确方法是什么。我试过这个:if(agents[i].PoliciesInForce.Daily[i] != null)
  • 是的,您可以检查 null,如果不为 null,请使用它。但实际上这取决于您的程序的需求。
  • 嗨朋友,我发布了我认为现在有效的编辑。请借给我您对此有帮助的见解,谢谢。
猜你喜欢
  • 2013-11-28
  • 2017-10-27
  • 2021-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-20
相关资源
最近更新 更多