C# 比较反序列化Json对象中的值

C# 比较反序列化Json对象中的值,c#,json,api,linq,deserialization,C#,Json,Api,Linq,Deserialization,我有一个API,它可以从各种来源编译类似的信息。我需要做的是比较这些来源,并将置信度最高的来源保存为一个变量。我可以想出一些冗长的方法来实现这一点,创建多个数组或列表,并对每个数组或列表进行迭代,直到得到最高值,但我想知道是否有更简单的方法使用linq来实现这一点 { "modeled_response": [ { "source": "arturo_bulk_import", "attri

我有一个API,它可以从各种来源编译类似的信息。我需要做的是比较这些来源,并将置信度最高的来源保存为一个变量。我可以想出一些冗长的方法来实现这一点,创建多个数组或列表,并对每个数组或列表进行迭代,直到得到最高值,但我想知道是否有更简单的方法使用linq来实现这一点

{
  "modeled_response": [
    {
      "source": "arturo_bulk_import",
      "attributes": {
        "roof_shape": {
          "value": "Gable",
          "confidence": 0.9522576226679909
        },
        "roof_slope": {
          "value": "Low",
          "confidence": 0.8674100762576565
        }
    }
    },
    {
      "source": "region_state_arturo_bulk_import",
      "attributes": {
        "roof_shape": {
          "value": "Gable",
          "confidence": 0.8467693167596497
        },
        "roof_slope": {
          "value": "Low",
          "confidence": 0.8515481815500642
        }
}
    },
    {
      "source": "region_zipcode5_arturo_bulk_import",
      "attributes": {
        "roof_shape": {
          "value": "Gable",
          "confidence": 0.8353433674418161
        },
        "roof_slope": {
          "value": "Low",
          "confidence": 0.868985703016765
        }
      }
    }
  ],
}
然后我反序列化对象并将其保存到结果列表中。不确定从何处获取具有最大置信度的值

    class Program
    {
        static void Main(string[] args)
        {

            const string FILE_PATH = @"";

            StreamReader r = new StreamReader(FILE_PATH);
            string json = r.ReadToEnd();
            RootObject deserializedProduct = 
                JsonConvert.DeserializeObject
                    <RootObject>(json);

            List<RoofShape> resultsList = new List<RoofShape>();
            int index = 0;
            do
            {
                resultsList.Add(new RoofShape
                {
                    confidence = deserializedProduct.modeled_response[index]
                    .attributes.roof_shape.confidence,
                    value = deserializedProduct.modeled_response[index]
                    .attributes.roof_shape.value
                });
                index++;
            } while (deserializedProduct.modeled_response.Count > index);
        }
    }

    public class RootObject
    {
        public string normalized_address { get; set; }
        public string created_timestamp { get; set; }
        public List<ModeledResponse> modeled_response { get; set; } 
    }

    public class ModeledResponse
    {
        public string source { get; set; }
        public Attributes attributes { get; set; }
        
    }

    public class Attributes
    {
        public string attributes { get; set; }
        public RoofShape roof_shape { get; set; }
    }

    public class RoofShape
    {
        public string value { get; set; }
        public decimal confidence { get; set; }
    }
类程序
{
静态void Main(字符串[]参数)
{
常量字符串文件_路径=@“”;
StreamReader r=新的StreamReader(文件路径);
字符串json=r.ReadToEnd();
RootObject反序列化的产品=
JsonConvert.DeserializeObject
(json);
列表结果列表=新列表();
int指数=0;
做
{
结果列表。添加(新屋顶形状)
{
信心=反序列化的产品。建模的\u响应[索引]
.attributes.roof_shape.confidence,
值=反序列化的产品。建模的\u响应[索引]
.attributes.roof_shape.value
});
索引++;
}while(反序列化的dproduct.modeled_response.Count>index);
}
}
公共类根对象
{
公共字符串规范化_地址{get;set;}
创建的公共字符串\u时间戳{get;set;}
公共列表\u响应{get;set;}
}
公共类模型响应
{
公共字符串源{get;set;}
公共属性{get;set;}
}
公共类属性
{
公共字符串属性{get;set;}
公共屋顶形状屋顶形状{get;set;}
}
公共类屋顶形状
{
公共字符串值{get;set;}
公共十进制置信度{get;set;}
}

下面的代码使用Linq生成具有最高置信水平的
ModeledResponse
变量

RootObject deserializedProduct = JsonConvert.DeserializeObject<RootObject>(json);
decimal maxConfidenceInAllModels = deserializedProduct.modeled_response.Max(x => x.attributes.roof_shape.confidence);
ModeledResponse confident = deserializedProduct.modeled_response
                            .Where(x => x.attributes.roof_shape.confidence == maxConfidenceInAllModels)
                            .FirstOrDefault();
RootObject deserializedProduct=JsonConvert.DeserializeObject(json);
decimal maxConfidenceInAllModels=deserializedProduct.modeled_response.Max(x=>x.attributes.roof_shape.confidence);
ModeledResponse confidence=反序列化的产品。ModeledResponse\u响应
.Where(x=>x.attributes.roof_shape.confidence==maxConfidenceInAllModels)
.FirstOrDefault();

使用
Max
方法,您可以找出最高置信水平。。然后使用该数字筛选出列表。

反序列化的产品.modeled\u响应.OrderByDescending(x=>x.attributes.roof\u shape.confidence)。FirstOrDefault()?也可以。我建议你补充这一点作为对这个问题的回答。