麻烦解析这个json

时间:2017-08-18 20:00:53

标签: c# json api parsing

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

def recommendation(word):
    n = 3
    # n means 'n'-grams, here I use 3 as an example
    spellings_new = [w for w in spellings if (w[0] == word[0])]
    dists = [jaccard_distance(set(w), set(word)) for w in spellings_new]
    return spellings_new[dists.index(min(dists))]

}

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

1 个答案:

答案 0 :(得分: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
    });
}