繁体   English   中英

比较反序列化 Json object 的值

[英]Comparing values from a Deserialized Json object

我有一个 API 编译来自各种来源的类似信息。 我需要做的是比较这些来源并将具有最高置信度的来源保存到变量中。 我可以想到一些冗长的方法来实现这一点,创建多个 arrays 或列表并遍历每个列表,直到我得到最高值,但我想知道是否有更简单的方法来使用 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
        }
      }
    }
  ],
}

然后我反序列化 object 并将其保存到结果列表中。 不确定从这里到 go 从哪里检索具有最大置信度的值

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

以下代码使用 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();

使用Max方法,您可以找出最高置信度是多少......然后使用该数字过滤掉列表。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM