gpt4 book ai didi

c# - 如何使用Newtonsoft Deserialize C#反序列化JSON

转载 作者:行者123 更新时间:2023-12-03 10:32:15 25 4
gpt4 key购买 nike

我正在尝试反序列化JSON文件,并希望分配给对象ScanResult。 var text显示所有值,但scanresult显示null某些null值。 https://gyazo.com/ff2ce386f845394c458a88d43a1f30d8

请建议我是否缺少任何东西。
//MY jSon File SCAN Test 1-1543045410222.json的代码

{
"at": 1543045410222,
"i": 1000,
"s": {
"Sensor1": ["OFF"],
"Sensor2": ["OFF"],
"DataReady1": ["OFF"],
"DataReady2": ["OFF"],
"CV1": [5.0],
"CV2": [6.0]
}
}




//ViewModel Code is as below:

public void ResendScanResult()
{
var ScanActivities = scanActivityManager.GetAll();
foreach (var item in ScanActivities)
{
var scanName = item.ScanName;
var dir = _dataFilePath + scanName + "\\";
var jsonFileName = string.Format("{0}{1}-{2}.json", dir, scanName, item.ScanDateEpoch);
string fileName = Path.GetFileName(jsonFileName);
// ScanResult scanResult = new ScanResult();
var text = File.ReadAllText(jsonFileName);
//var scanResults = JsonConvert.DeserializeObject<ScanResult>(text);
Common.Model.ScanResult scanResult = JsonConvert.DeserializeObject<Common.Model.ScanResult>(text);


var Mvm = MonitorViewModel.Instance;
// TargetProvider target = Mvm.GetTargetProvider(scanResult);
// Mvm.PublishToServer(target, scanResult);
}
}

我的scanRescult类代码如下:
namespace ABX.Common.Model
{
public class ScanResult
{
public ScanResult()
{
At = DateTimeOffset.Now.ToUnixTimeMilliseconds();
Interval = 1;
}

public string Name { get; set; }
public long At { get; set; }
public long Interval { get; set; }
public JObject Values { get; set; }
public string FileName { get; set; }

public JObject ToJson()
{
JObject json = new JObject
{
{ "at", At },
{ "i", Interval },
{ "s", Values }
};
return json;
}

最佳答案

重命名类属性以匹配JSON,重命名JSON以匹配类属性,或者实现自定义JsonConverter,您可以在其中实现任意映射。

关于c# - 如何使用Newtonsoft Deserialize C#反序列化JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53625440/

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