我尝试从 Newtonsoft.Json 迁移到 System.Text.Json。我想反序列化抽象类。Newtonsoft.Json 为此具有 TypeNameHandling。有没有办法通过.net core 3.0 上的 System.Text.Json 反序列化抽象类?
我有一个属性类型为 的 DTO 类JObject。此 DTO 类在多个服务之间通过 HTTP 发送/接收。使用 JObject 是因为ExtractedData它没有预定义的属性
public class MyDTO
{
public JObject ExtractedData {get;set;}
}
Run Code Online (Sandbox Code Playgroud)
我正在将此项目转换为 .NET 5。什么相当于 .NET 5 中的 JObject?我试图避免 JsonDocument 因为(来自文档):
JsonDocument 将数据的内存视图构建到池化缓冲区中。因此,与 Newtonsoft.Json 中的 JObject 或 JArray 不同,JsonDocument 类型实现 IDisposable 并且需要在 using 块中使用。
我打算使用JsonElement. 这是最合适的选择还是有任何其他类型可用于将 JSON 作为对象保存?
假设我有一个类型的对象:
public class MyClass
{
public string Data { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我需要将其转换为 System.Text.Json.JsonElement。我找到的唯一方法是:
var json = JsonSerializer.Serialize(new MyClass { Data = "value" });
using var document = JsonDocument.Parse(json);
var jsonElement = document.RootElement;
Run Code Online (Sandbox Code Playgroud)
我必须先序列化它然后再解析它,这似乎很奇怪。有没有更好的方法呢?
以前我是JObject从 Newtonsoft.Json使用的,我可以这样做:
var jobject = JObject.FromObject(new MyClass { Data = "value" });
Run Code Online (Sandbox Code Playgroud) 我不相信我正在考虑如何在解析 json 结果时正确使用 JsonConverter 进行多态性。
在我的场景中,我的目标是 TFS 中的 Git 策略配置。策略配置:
"value": [
{
"createdBy": {
"displayName": "username",
"url": "url",
"id": "id",
"uniqueName": "user",
"imageUrl": "url"
},
"createdDate": "2020-03-21T18:17:24.3240783Z",
"isEnabled": true,
"isBlocking": true,
"isDeleted": false,
"settings": {
"minimumApproverCount": 1,
"creatorVoteCounts": false,
"allowDownvotes": false,
"resetOnSourcePush": true,
"scope": [{
"refName": "refs/heads/master",
"matchKind": "Exact",
"repositoryId": "id"
}
]
},
"_links": {
"self": {
"href": "url"
},
"policyType": {
"href": "url"
}
},
"revision": 1,
"id": 974,
"url": "url",
"type": {
"id": "id",
"url": "url", …Run Code Online (Sandbox Code Playgroud) 我开始 C# 并反序列化 Json。在我的培训中,我学习了 Newtonsoft,但我想对 system.text.json 做同样的事情
有了这个 json,我想选择
制作一个对象列表。
https://api.nvidia.partners/edge/product/search?page=1&limit=9&locale=fr-fr&category=GPU&gpu=RTX%203090,RTX%203080%20Ti,RTX%203080,RTX%203070%20Ti,RTX%203070,RTX%203060%20Ti,RTX%203060&gpu_filter=RTX%203090~12,RTX%203080%20Ti~7,RTX%203080~16,RTX%203070%20Ti~3,RTX%203070~18,RTX%203060%20Ti~8,RTX%203060~2,RTX%202080%20SUPER~1,RTX%202080~0,RTX%202070%20SUPER~0,RTX%202070~0,RTX%202060~6,GTX%201660%20Ti~0,GTX%201660%20SUPER~9,GTX%201660~8,GTX%201650%20Ti~0,GTX%201650%20SUPER~3,GTX%201650~17
Run Code Online (Sandbox Code Playgroud)
班级
public class CarteGraphique
{
public string displayName { get; set; }
public string prdStatus { get; set; }
public List<Retailer> retailers { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
使用 Newtonsoft,我执行以下操作:
牛顿软件
JObject jsonParse = JObject.Parse(json);
IList<CarteGraphique> products = new List<CarteGraphique>();
IList<JToken> productDetailsParse = jsonParse["searchedProducts"]["productDetails"]
.Children()
.Where(n => n["isFounderEdition"].Value<bool>() == true)
.ToList();
var featuredProductParse = jsonParse["searchedProducts"]["featuredProduct"];
foreach (JToken item in productDetailsParse)
{
CarteGraphique result …Run Code Online (Sandbox Code Playgroud) 使用 Newtonsoft Json,您可以JObject通过调用将对象转换为JObject.FromObject(object).
System.Text.Json 中是否有JsonDocument从对象中获取 a 的对应项?
我正在尝试创建一个在 .net core 3.1 中呈现的页面,该页面基于 JSON 呈现页面。
如何反序列化本文末尾的 JSON?
我尝试反序列化它,但它不起作用,因为我丢失了每个组件的数据,因为 Page 类有一个List<Component>- 但我需要它是不同组件的列表。
页面模型:
public class Page
{
public int id { get; set; }
public string pagename { get; set; }
public string metatitle { get; set; }
public string metadescription { get; set; }
public string created_at { get; set; }
public string updated_at { get; set; }
public List<Component> components { get; set; }
}
public class Pages
{
public List<Page> pages { get; set; …Run Code Online (Sandbox Code Playgroud)