我在读取一个特定的JSON文件时遇到了一些问题。我的代码正在读取其他JSON文件,所以我猜我正在做一些非常愚蠢的事情,但我已经盯着它看了大约一个小时,但没有成功。我有以下代码
List<VFData> visualFieldData;
public class VFData
{
public double x { get; set; }
public double y { get; set; }
public string type { get; set; }
public double contrast { get; set; }
}
private void LoadVFDataJSONFile(string VFdataFile )
{
string jsonStream;
using (StreamReader r = new StreamReader(VFdataFile))
{
jsonStream = r.ReadToEnd();
}
visualFieldData = JsonConvert.DeserializeObject<List<VFData>>(jsonStream);
}
The JSON file has the following format
[
{
"x": -4.0,
"y": 4.0,
"type": "vf",
"contrast": 0.670277655
},
{
"x": -14.0,
"y": 4.0,
"type": "vf",
"contrast": 0.407872766
},
{
"x": -24.0,
"y": 4.0,
"type": "vf",
"contrast": 0.372877628
},
{
"x": -34.0,
"y": 4.0,
"type": "vf",
"contrast": 0.4652638
},
{
"x": -4.0,
"y": 14.0,
"type": "vf",
"contrast": 0.399946272
},
{
"x": -4.0,
"y": 24.0,
"type": "vf",
"contrast": 0.420959234
},
{
"x": -14.0,
"y": 14.0,
"type": "vf",
"contrast": 0.3694598
},
{
"x": -14.0,
"y": 24.0,
"type": "vf",
"contrast": 0.4507869
},
{
"x": -24.0,
"y": 14.0,
"type": "vf",
"contrast": 0.4334121
},
{
"x": 4.0,
"y": 4.0,
"type": "vf",
"contrast": 0.515925348
},
{
"x": 14.0,
"y": 4.0,
"type": "vf",
"contrast": 0.487090856
},
{
"x": 24.0,
"y": 4.0,
"type": "vf",
"contrast": 0.5159793
},
{
"x": 4.0,
"y": 14.0,
"type": "vf",
"contrast": 0.3921606
},
{
"x": 4.0,
"y": 24.0,
"type": "vf",
"contrast": 0.4164949
},
{
"x": 14.0,
"y": 14.0,
"type": "vf",
"contrast": 0.392385662
},
{
"x": 14.0,
"y": 24.0,
"type": "vf",
"contrast": 0.426395863
},
{
"x": 24.0,
"y": 14.0,
"type": "vf",
"contrast": 0.4305214
},
{
"x": 4.0,
"y": -4.0,
"type": "vf",
"contrast": 0.377473235
},
{
"x": 14.0,
"y": -4.0,
"type": "vf",
"contrast": 0.355986029
},
{
"x": 24.0,
"y": -4.0,
"type": "vf",
"contrast": 0.3789097
},
{
"x": 4.0,
"y": -14.0,
"type": "vf",
"contrast": 0.396197081
},
{
"x": 4.0,
"y": -24.0,
"type": "vf",
"contrast": 0.363053441
},
{
"x": 14.0,
"y": -14.0,
"type": "vf",
"contrast": 0.384389132
},
{
"x": 14.0,
"y": -24.0,
"type": "vf",
"contrast": 0.440146685
},
{
"x": 24.0,
"y": -14.0,
"type": "vf",
"contrast": 0.387488931
},
{
"x": -4.0,
"y": -4.0,
"type": "vf",
"contrast": 0.366485029
},
{
"x": -14.0,
"y": -4.0,
"type": "vf",
"contrast": 0.392450541
},
{
"x": -24.0,
"y": -4.0,
"type": "vf",
"contrast": 0.428462267
},
{
"x": -34.0,
"y": -4.0,
"type": "vf",
"contrast": 0.394372374
},
{
"x": -4.0,
"y": -14.0,
"type": "vf",
"contrast": 0.377464861
},
{
"x": -4.0,
"y": -24.0,
"type": "vf",
"contrast": 0.372114331
},
{
"x": -14.0,
"y": -14.0,
"type": "vf",
"contrast": 0.377241641
},
{
"x": -14.0,
"y": -24.0,
"type": "vf",
"contrast": 0.42327565
},
{
"x": -24.0,
"y": -14.0,
"type": "vf",
"contrast": 0.407872945
}
]我得到以下错误
'System.Collections.Generic.List`1WindowsFormsApp1.Form1+VFData‘:’无法将当前JSON对象(例如,{“名称”:“值”})反序列化为类型Newtonsoft.Json.JsonSerializationException,因为该类型需要一个JSON数组(例如,1、2、3)才能正确反序列化。要修复此错误,要么将JSON更改为JSON数组(例如,1,2,3),要么更改反序列化类型,使之成为可以从JSON对象反序列化的普通.NET类型(例如,不像整数这样的原始类型,而不是数组或列表之类的集合类型)。还可以将JsonObjectAttribute添加到类型中,以强制它从JSON对象反序列化。路径'x',第1行,位置5‘
发布于 2022-11-27 22:54:10
总是检验你的假设
var jsonData = @"[
{
""x"": -4.0,
""y"": 4.0,
""type"": ""vf"",
""contrast"": 0.670277655
},
{
""x"": -14.0,
""y"": 4.0,
""type"": ""vf"",
""contrast"": 0.407872766
},
{
""x"": -24.0,
""y"": -14.0,
""type"": ""vf"",
""contrast"": 0.407872945
}
]";
var visualFieldData = JsonConvert.DeserializeObject<List<VFData>>(jsonData);上面的代码运行良好,没有任何例外(我用完整的json示例进行了测试,只是为了得到答案而缩短了它)。我建议在调试模式下运行,查看实际要转换的字符串,或者添加某种日志语句来显示json字符串。
我们无法知道您是否读取了错误的文件,该文件是否已损坏字符,或者是否正在发生其他问题--但我可以肯定的一点是,您问题中的JSON在反序列化类中运行良好
编辑:看看我猜到的错误,下面的代码会产生相同的错误
try
{
var jsonData = @"
{
""x"": -4.0,
""y"": 4.0,
""type"": ""vf"",
""contrast"": 0.670277655
}";
var visualFieldData = JsonConvert.DeserializeObject<List<VFData>>(jsonData);
}
catch (Exception ex)
{
logger.Info(ex.Message);
logger.Info(ex.StackTrace);
}
}它可能是您的文件有一个项目,而不是一个项目数组。解决方案只是反序列化到VFData的单个实例,而不是列表。
var visualFieldData = JsonConvert.DeserializeObject<VFData>(jsonData);https://stackoverflow.com/questions/74593920
复制相似问题