---
# System prepended metadata

title: C# Json檔轉成物件Class
tags: [C#]

---

### Json檔利用LINQ轉成物件Class
DataConfig 是你的Json檔物件
```
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

var JsonPath = Path.Combine(Environment.CurrentDirectory, "DataConfig.json");
var Jobj = JObject.Parse(System.IO.File.ReadAllText(JsonPath, Encoding.Default));
//Json轉Class
DataConfig oDataConfig = Jobj.ToObject<DataConfig>();
```

DataConfig.cs
```
public class DataConfig
{
        public string NotifyToken { get; set; }
        public List<CameraInfo> CamInfoList { get; set; }
}
public class CameraInfo
{
        public string RTSPUrl { get; set; }
        public string CamNo { get; set; }
        public string CamName { get; set; }
        public int OfflineTimeSecond { get; set; }
        public bool IsNotify { get; set; }
}
```

DataConfig.json
```
{
  "NotifyToken": "XXX",
  "CamInfoList": [
    {
      "RTSPUrl": "rtsp://XXX",
      "CamNo": "Cam_1", /*攝影機識別No*/
      "CamName": "A1", 
      "OfflineTimeSecond": 60, 
      "IsNotify": true 
    },
    {
      "RTSPUrl": "rtsp://XXX",
      "CamNo": "Cam_2", 
      "CamName": "A2", 
      "OfflineTimeSecond": 60,
      "IsNotify": true 
    },
  ]
}
```