如何在我的 Web 服务 POST 方法中反序列化 JSON

本文关键字:方法 反序列化 JSON POST 服务 我的 Web | 更新日期: 2023-09-27 18:30:58

我的 Web 服务接收 JSON 格式的数据过滤值。下面是如何发送筛选条件的示例:

var jsonString = {
"startDate":"2015-01-19T15:04:54.897Z",
"endDate":"2016-01-19T15:04:54.897Z",
"filterParams":{
    "facilityIds":
        [
            {"ID":1,"charID":"1","Description":"Health Group Umbrella Organization","DoubleValue1":35.009803,"DoubleValue2":85.21,"StringValue1":""},
            {"ID":2,"charID":"2","Description":"Main Hospital Organization","DoubleValue1":35.04,"DoubleValue2":89.3,"StringValue1":""},
            {"ID":3,"charID":"3","Description":"Regional Medical Center","DoubleValue1":35.04,"DoubleValue2":89.30,"StringValue1":""}
        ],                      
    "GenderIds":[{"ID":0,"charID":"F","Description":"Female","DoubleValue1":0,"DoubleValue2":0,"StringValue1":""}],
    "AgeRangeIds":[{"ID":4,"charID":"4","Description":"30 - 34","DoubleValue1":0,"DoubleValue2":0,"StringValue1":""}],
    "HomeownerIds":[{"ID":7,"charID":"7","Description":"More Likely Owner 1","DoubleValue1":0,"DoubleValue2":0,"StringValue1":""}],
    "IncomeRangeIds":[{"ID":3,"charID":"3","Description":"$30,001 - $40,000","DoubleValue1":0,"DoubleValue2":0,"StringValue1":""}],
    "HasChildrenIds":[{"ID":0,"charID":"N","Description":"No","DoubleValue1":0,"DoubleValue2":0,"StringValue1":null}],
    "EncoutnerTypesIds":
        [
            {"ID":2,"charID":"2","Description":"Ambulatory","DoubleValue1":0,"DoubleValue2":0,"StringValue1":""},
            {"ID":1,"charID":"1","Description":"InPatient","DoubleValue1":0,"DoubleValue2":0,"StringValue1":""},
            {"ID":3,"charID":"3","Description":"Emergency","DoubleValue1":0,"DoubleValue2":0,"StringValue1":""},
            {"ID":4,"charID":"4","Description":"Office Visit","DoubleValue1":0,"DoubleValue2":0,"StringValue1":""}
        ]
    }
}

我可以拉出开始日期和结束日期没有问题:

var jsonValues = jsonSerializer.Deserialize<Dictionary<string, object>>(jsonString);            
DateTime startDate = DateTime.Parse(jsonValues["startDate"] as string);
DateTime endDate = DateTime.Parse(jsonValues["endDate"] as string);

但是,我似乎无法弄清楚如何处理 JSON 的"filterParams"部分。它应该映射到类结构:

public class FilterModel
{
    public int ID { get; set; }
    public char charID { get; set; }
    public string Description { get; set; }
    public double DoubleValue1 { get; set; }
    public double DoubleValue2 { get; set; }
    public string StringValue1 { get; set; }
}

以下语句使用正确的数据填充变量,但我无法访问这些值。

var filterParams = jsonValues["filterParams"];

我需要以某种方式将数据反序列化为Dictionary<string, FilterModel>但反序列化方法需要一个字符串参数,而 filterParams 是一个对象。

如何在我的 Web 服务 POST 方法中反序列化 JSON

您不需要手动反序列化每个属性...

JavaScriptSerializer

您可以使用本机JavaScriptSerializer将其直接反序列化为复杂对象:

RootObject obj = new JavaScriptSerializer().Deserialize<RootObject>(jsonString);

牛顿软件 Json

或者按照@Wobbles的建议,您可以使用Json.Net也称为Newtonsoft Json:

RootObject obj = JsonConvert.DeserializeObject<RootObject>(jsonString);

正如您在此处看到的,它比JavaScriptSerializer更快,功能更多。

你可以使用这样的东西(非常具体,但它必须有效):

-> 这是使用牛顿软件

    public class YourJson
    {
        public DateTime startDate;
        public DateTime endDate;
        public FilterParams filterParams;
    }
    public class FilterParams
    {
        public __ID[] facilityIds;
        public __ID[] GenderIds;
        public __ID[] AgeRangeIds;
        public __ID[] HomeownerIds;
        public __ID[] IncomeRangeIds;
        public __ID[] HasChildrenIds;
        public __ID[] EncoutnerTypesIds;
    }
    public class __ID
    {
        public int ID;
        public char charID;
        public string Description;
        public double DoubleValue1;
        public double DoubleValue2;
        public String StringValue1;
    }
    var yourJson = JsonConvert.DeserializeObject<YourJson>(json);