c# - REST API 反序列化 Newtonsoft.Json 中的嵌套响应

编辑:一旦我反序列化了这些信息,我怎样才能访问我需要的数据?我需要获取的唯一信息是每个附件的 TransitTime 和 Cost。

我正在使用一个 API,其中我需要来自嵌套对象的数据,但我不确定如何访问这些信息。我已将数据传递给 API,并根据数据从 API 返回的方式设置了新类。

这是响应的样子:

{"RatingResponse":
  {"Success":"true",
  "Message":"",
  "QuoteID":"57450",
  "LoadNum":"57450",

  "Rates":{
    "Rate":[
        {"SCAC":"TEST",
        "CarrierName":"TEST",
        "TransitTime":"1",
        "ServiceLevel":"D",
        "TotalCost":"983.69",
        "ThirdPartyCharge":"983.69",
        "Accessorials":{
            "Accessorial":[
                {"Code":"400",
                "Cost":"1,655.55",
                "Description":"Freight"
                },
                
                {"Code":"DSC",
                "Cost":"-985.55",
                "Description":"Discount"
                },
                
                {"Code":"FUE",
                "Cost":"313.69",
                "Description":"Fuel Surcharge"
                }
                ]
            },

        "QuoteNumber":""
        },
        
        {"SCAC":"TEST2",
         "CarrierName":"TEST2",
         "TransitTime":"1",
         "ServiceLevel":"D",
         "TotalCost":"983.69",
         "ThirdPartyCharge":"983.69",
         "Accessorials":{
            "Accessorial":[
                {"Code":"400",
                "Cost":"1,655.55",
                "Description":"Freight"
                },
                
                {"Code":"DSC",
                "Cost":"-985.55",
                "Description":"Discount"
                },
                
                {"Code":"FUE",
                "Cost":"313.69",
                "Description":"Fuel Surcharge"
                }
                ]
            },
        
        "QuoteNumber":""
        }
    ]
    },
"AverageTotalCost":"983.69"
}

}

我已将其转换为 C#:

public class Accessorial
{
    public string Code;
    public string Cost;
    public string Description;
}

public class Accessorials
{
    public List<Accessorial> Accessorial;
}

public class Rate
{
    public string SCAC;
    public string CarrierName;
    public string TransitTime;
    public string ServiceLevel;
    public string TotalCost;
    public string ThirdPartyCharge;
    public Accessorials Accessorials;
    public string QuoteNumber;
}

public class Rates
{
    public List<Rate> Rate;
}

public class RatingResponse
{
    public string Success;
    public string Message;
    public string QuoteID;
    public string LoadNum;
    public Rates Rates;
    public string AverageTotalCost;
}

public class Root
{
    public RatingResponse RatingResponse;
}

我需要的唯一 values 是费率运输时间和服务水平以及辅助成本。我是 API 的初学者,不知道如何只返回这些信息。任何帮助是极大的赞赏!

回答1

如果您使用此代码,则不需要任何课程

var rate = (JArray)JObject.Parse(json)["RatingResponse"]["Rates"]["Rate"];
    var result = rate.Select(r => new
    {
        TransitTime = (int)r["TransitTime"],
        ServiceLevel = (string) r["ServiceLevel"],
        AccessorialCost = ((JArray)r["Accessorials"]["Accessorial"]).Select(r => (double)r["Cost"]).ToList()
    });

结果(json格式)

[
  {
    "TransitTime": 1,
    "ServiceLevel": "D",
    "AccessorialCost": [
      1655.55,
      -985.55,
      313.69
    ]
  },
  {
    "TransitTime": 1,
    "ServiceLevel": "D",
    "AccessorialCost": [
      1655.55,
      -985.55,
      313.69
    ]
  }
]

或者您可以创建一个数据类而不是匿名的

List<Data> result = rate.Select(r => new Data 
    {
     ....
    }).ToList();

public class Data
{
    public int TransitTime { get; set; }
    public string ServiceLevel { get; set; }
    public List<double> AccessorialCost { get; set; }
}

回答2

我喜欢@Serge 的回答,但我更喜欢在课堂上取得成绩,因为“我只需要......”永远不会持续很长时间,对吗?下面是关于将 JSON 加载到 https://stackoverflow.com/questions/48585029/deserialize-multiple-json-into-object-c-sharp 的一个很好的讨论:您的 JSON 在 JSON2C#.com 中出错,但本质上您的根对象需要其他类。就像是

public class Root
{
    public RatingResponse RatingResponse;
    public Rates Rates;
    public Accessorials Accessorials;
}

然后反序列化为您的复杂对象

JsonConvert.DeserializeObject<RootObject>(json)

我正在做这件事,所以请原谅任何语法错误。

相似文章