提问人:Majid Qureshi 提问时间:11/2/2023 最后编辑:O. JonesMajid Qureshi 更新时间:11/2/2023 访问量:84
如何在具有多个嵌套模型的 C# 中解析 Json
How to parse Json in C# which has multiple nested models
问:
我正在尝试找出逻辑来编写一种方法来解析 Json 格式的数据。
{
"meals": [
{
"id": 279205,
"isDeleted": false,
"image": "https://d15rxdoyo1waj2.cloudfront.net/production/image?b=snaq-app-production&i=meals/279204/image.jpg&w=1200&q=90",
"mealName": "Smoked salmon ala Mamma",
"mealTime": "2023-09-08T14:17:52Z",
"ingredients": [
{
"name": "Carrot"
},
{
"name": "Lemon"
},
{
"name": "Arugula"
},
{
"name": "Salmon Grilled"
}
],
"nutrition": {
"carbohydrates": {
"unit": "g",
"value": 8
},
"fatTotal": {
"unit": "g",
"value": 22
},
"protein": {
"unit": "g",
"value": 34
},
"dietryFibers": {
"unit": "g",
"value": 4
},
"energy": {
"unit": "kcal",
"value": 365
}
},
"weight": {
"unit": "g",
"value": 302
}
}
],
"insulinDelivery": [
{
"id": 123456,
"isDeleted": false,
"injectionTime": "2023-09-09T13:12:12Z",
"insulinType": "SHORT",
"unit": "units",
"value": 5.0
}
],
"workouts": [
{
"id": 10223,
"isDeleted": false,
"startDate": "2023-09-10T10:52:23Z",
"endDate": "2023-09-10T11:35:45Z",
"intensity": "EASY",
Datastructure 2
"workoutName": "Baseball"
}
],
"steps": [
{
"startDate": "2023-09-18T14:00:00Z",
"endDate": "2023-09-18T15:00:00Z",
"stepCount": "1500"
}
]
}
我正在考虑如何编写方法在 C 中解析这个 Json 有效载荷#
答:
1赞
basilicon
11/2/2023
#1
查看 Newtonsoft 的 JSON 库。它可以让你直接反序列化为对象。以下是他们网站的示例:
public class Account
{
public string Email { get; set; }
public bool Active { get; set; }
public DateTime CreatedDate { get; set; }
public IList<string> Roles { get; set; }
}
Account account = JsonConvert.DeserializeObject<Account>(json);
如果你想避免直接反序列化到你的类中(例如,如果你要有私有变量),你可以做一些事情,比如:
Dictionary<string, object> jsonObject = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonString);
评论
0赞
Majid Qureshi
11/2/2023
我们是否先反序列化,然后解析它?还是在反序列化后,我们不需要解析?
0赞
Allen King
11/2/2023
#2
'myJsonResponse 是你的 json 字符串(检查你的 json,该行似乎不适合那里)DataStructure 2
Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class Carbohydrates
{
public string unit { get; set; }
public int value { get; set; }
}
public class DietryFibers
{
public string unit { get; set; }
public int value { get; set; }
}
public class Energy
{
public string unit { get; set; }
public int value { get; set; }
}
public class FatTotal
{
public string unit { get; set; }
public int value { get; set; }
}
public class Ingredient
{
public string name { get; set; }
}
public class InsulinDelivery
{
public int id { get; set; }
public bool isDeleted { get; set; }
public DateTime injectionTime { get; set; }
public string insulinType { get; set; }
public string unit { get; set; }
public double value { get; set; }
}
public class Meal
{
public int id { get; set; }
public bool isDeleted { get; set; }
public string image { get; set; }
public string mealName { get; set; }
public DateTime mealTime { get; set; }
public List<Ingredient> ingredients { get; set; }
public Nutrition nutrition { get; set; }
public Weight weight { get; set; }
}
public class Nutrition
{
public Carbohydrates carbohydrates { get; set; }
public FatTotal fatTotal { get; set; }
public Protein protein { get; set; }
public DietryFibers dietryFibers { get; set; }
public Energy energy { get; set; }
}
public class Protein
{
public string unit { get; set; }
public int value { get; set; }
}
public class Root
{
public List<Meal> meals { get; set; }
public List<InsulinDelivery> insulinDelivery { get; set; }
public List<Workout> workouts { get; set; }
public List<Step> steps { get; set; }
}
public class Step
{
public DateTime startDate { get; set; }
public DateTime endDate { get; set; }
public string stepCount { get; set; }
}
public class Weight
{
public string unit { get; set; }
public int value { get; set; }
}
public class Workout
{
public int id { get; set; }
public bool isDeleted { get; set; }
public DateTime startDate { get; set; }
public DateTime endDate { get; set; }
public string intensity { get; set; }
public string workoutName { get; set; }
}
0赞
user16606026
11/2/2023
#3
1. 序列化?反序列化?
TL;DR:Serialize=object 到 json,Deserialize=json 到 object。
2. 我们需要将json反序列化为对象吗?
如果您使用的是 ASP.NET / ASP.NET Core,应用程序将为您处理它,但您需要定义控制器,并告诉应用程序您期望什么输入,从类的意义上讲,它会自动反序列化为该类型。
Allen King 似乎已经提供了必要的类,因此您可以在此基础上构建控制器:
public class HelloWorldController : Controller
{
public IActionResult SomeEndpoint(Root root)
{
// If input format is as expected, ASP.NET will deserialize the json into the root in this function's parameter.
DoSomething(root); // you then use the root.
}
}
评论