gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 19:54:50 25 4
gpt4 key购买 nike

我的网络服务接收 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":""}
]
}
}

我可以毫无问题地取出 startDate 和 endDate:

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>但是 Deserialize 方法需要一个字符串参数,而 filterParams 是一个对象。

最佳答案

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

JavaScriptSerializer

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

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

Newtonsoft JSON

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

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

如您所见here,它比 JavaScriptSerializer 速度更快且功能更多.

关于c# - 如何在我的 Web 服务 POST 方法中反序列化 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34884848/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com