尝试解码JSON对象时出错

时间:2016-12-20 04:36:53

标签: c#

我正在尝试解码JSON对象' BuildTestPlans'找出不同的BuildID并保存所有记录(例如,BuildID 0有2条记录)与之对应,但遇到以下错误,如何解决?

BuildTestPlans:
[
  {
    "BuildTestPlanID": 0,
    "BuildID": 0,
    "Build": "string",
    "TestPlanMasterID": 0

  },
 {
    "BuildTestPlanID": 0,
    "BuildID": 0,
    "Build": "string1",
    "TestPlanMasterID": 0
  },
 {
    "BuildTestPlanID": 1,
    "BuildID": 1,
    "Build": "string",
    "TestPlanMasterID": 0
 }
]

public List<BuildTestPlan> AddOrUpdate(List<BuildTestPlan> BuildTestPlans)
{
    foreach (var BuildID in BuildTestPlans.BuildID.Distinct())
    {
        //save all records for each buildID
    }
    return null;
}

错误:

  

列表不包含BuildID的定义,也没有扩展方法&#39; BuildID&#39;接受类型&#39; List&#39;的第一个参数。可以找到(你错过了使用指令或程序集引用吗?)

3 个答案:

答案 0 :(得分:1)

假设BuildTestPlan.BuildIDint

// processed list, initially empty
List<int> buildList = new List<int>();

foreach (var buildTestPlan in BuildTestPlans)
{

    // if the BuildID was not previously processed
    if (buildList.IndexOf(buildTestPlan.BuildID) < 0){
        buildList.Add(buildTestPlan.BuildID);
        // todo: process the buildTestPlan for this newly found BuildID
    } 

    // if the BuildID was previously processed
    else {
        // todo: something else, or skip it
    }   
}
return null;

此处添加了关于您的评论 -

  

为BuildID

创建所有buildTestPlan的列表

假设您要为buildTestPlan提取BuildID = 0,它可能是您期望的列表。

var BuildTestPlans_WithParticularBuildId = BuildTestPlans.Select(c => c.BuildID == 0);

答案 1 :(得分:1)

BuildTestPlans列表显然没有BuildID,因为它是BuildTestPlans的List,而不是你可以访问它的对象,但像buildtestplans[0]这样的索引号是你可以访问它的方式

    public List<BuildTest> AddOrUpdate(List<BuildTest> BuildTestPlans)
    {
        List<int> BTPBuildID = new List<int>();
        foreach(var BuildTP in BuildTestPlans)
        {
             BTPBuildID.Add(BuildTP.BuildID);
        }
        BTPBuildID.Distinct();
        return null;
    }

答案 2 :(得分:-2)

你的json上有一个缺失的大括号,在结束之前这应该有效