简体   繁体   English

如何按部分将 JSON 文件反序列化为 C# object

[英]How to deserialize JSON file to C# object by section

I have 2 C# class:我有 2 个 C# class:

public class Light
{
    public int Brightness { get; set; }
    public string Mode { get; set; }
}

public class AirConditioner
{
    public int Temperature{ get; set; }
    public string Mode { get; set; }
}

JSON file format: JSON 文件格式:

{
  "Light": {
    "Brightness": 5,
    "Mode": "On"
  },
  "AirConditioner": {
    "Temperature": 25,
    "Mode": "Cooling"
  }
}

I want to parse JSON file to C# by section, something like this:我想按部分将 JSON 文件解析为 C#,如下所示:

var light = JsonDeserialize<Light>.(FileSection["Light"]);
var aircon = JsonDeserialize<AirConditioner>.(FileSection["AirConditioner"]);

What I want is the same as Asp.Net Core configuration work:我想要的和 Asp.Net Core 配置工作一样:

var light = new Light();
Configuration.GetSection("Light").Bind(light);

It will be better if I do not need to install other packages.如果我不需要安装其他软件包会更好。

Thank you for your help.谢谢您的帮助。

Update: The problem is how to get a section of the JSON file.更新:问题是如何获取 JSON 文件的一部分。 If I can get the Light section like this:如果我能得到这样的Light部分:

var lightString = JsonFile.GetSection("Light");

Then I can simply deserialize with System.Text.Json namespace:然后我可以简单地使用System.Text.Json命名空间反序列化:

var light = JsonSerializer.Deserialize<Light>(lightString);

I have done this by creating a parent class that holds the Light and AirConditioner properties.我通过创建一个包含 Light 和 AirConditioner 属性的父 class 来做到这一点。

The parent Class:父 Class:

public class Devices
{
    public Light Light {get;set;}
    public AirConditioner AirConditioner {get;set;}
}

Then you can de-serialize into the Devices and access the light and airconditioner.然后您可以反序列化到设备中并访问灯和空调。

var devices = JsonDeserialize<Devices>.(myjsonfilecontent);
var light = devices.Light;
var aircon = devices.AirConditioner;

I think you're looking for https://www.newtonsoft.com/json/help/html/SerializingJSONFragments.htm in Newtonsoft's JSON.Net我认为您正在 Newtonsoft 的 JSON.Net 中寻找https://www.newtonsoft.com/json/help/html/SerializingJSONFragments.htm

It allows for manipulating of a whole JSON object as very generic JOBject and locating and converting only the parts you need.它允许将整个 JSON object 操作为非常通用JOBject并仅定位和转换您需要的部分。

In your example this looks something like the following.在您的示例中,这类似于以下内容。 This code uses the newtonsoft.json package.此代码使用newtonsoft.json package。

using System;
using Newtonsoft.Json.Linq;

namespace SO_67293726
{

    public class Light
    {
        public int Brightness { get; set; }
        public string Mode { get; set; }
    }

    public class AirConditioner
    {
        public int Temperature { get; set; }
        public string Mode { get; set; }
    }

    class Program
    {
        public static string JsonString =>
@"{
  ""Light"": {
    ""Brightness"": 5,
    ""Mode"": ""On""
  },
  ""AirConditioner"": {
    ""Temperature"": 25,
    ""Mode"": ""Cooling""
  }
}";
        static void Main(string[] args)
        {
            var jobject = JObject.Parse(JsonString);
            var light = jobject["Light"].ToObject<Light>();
            var aircon = jobject["AirConditioner"].ToObject<AirConditioner>();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM