在 C# 中反序列化 JSON 个文件
Deserialising JSON files in C#
我不断收到以下错误:
Object reference not set to an instance of an object
这是我在 C:\part_param.json
文件中的 Json 字符串
{
"part_parameters" : {
"bar_diameter" : 300.4,
"core_height" : 132,
"roughing_offset" : 0.3
}
}
我使用的代码如下:
public class PLMpartParameter
{
public class Parameters
{
public float bar_diameter;
public float core_height;
public float roughing_offset;
public Parameters(float barD, float coreH, float roughingO)
{
bar_diameter = barD;
core_height = coreH;
roughing_offset = roughingO;
}
}
public Parameters parameters;
public PLMpartParameter(Parameters param)
{
parameters = param;
}
}
public static void LoadJson()
{
using (System.IO.StreamReader r = new System.IO.StreamReader(@"C:\part_param.json"))
{
string json = r.ReadToEnd();
_logger.Info(string.Format("Read entire file complete. File Values: {0}", json));
try
{
PLMpartParameter part = Newtonsoft.Json.JsonConvert.DeserializeObject<PLMpartParameter>(json);
}
catch (Exception e)
{
_logger.Info(string.Format("Read Json failed {0}", e.Message));
}
}
我在这里错过了什么?
我认为问题是你的 属性 被称为 'parameters' 但在你的 json 中它是 'part_parameters'.
您的代码存在一些问题:
- 您需要为 classes 使用默认构造函数(这是由于序列化程序如何处理类型 - 它们不会理解您的 class 特定的参数化构造函数)。
- 您的字段需要是可设置的属性(只需添加
{get;set;}
就可以了)。
- 我建议你用
[JsonProperty("part_parameters")]
修饰 parameters
属性 以获得反序列化
您期望的行为。
引用 Prasad Telkikar 的回答立即修复
Use json2csharp to get model for your json file, then deserialize your
json. You can use visual studio in build function to create class i.e.
Edit -> Paste special -> Paste JSON as Class
Here is class
> public class PartParameters {
> public double bar_diameter { get; set; }
> public int core_height { get; set; }
> public double roughing_offset { get; set; } }
>
> public class RootObject {
> public PartParameters part_parameters { get; set; } }
要反序列化,请使用以下代码
PLMpartParameter part =
Newtonsoft.Json.JsonConvert.DeserializeObject(json);
我的最终代码看起来像这样,并且可以正常工作!!
public class PartParameters
{
public double bar_diameter { get; set; }
public int core_height { get; set; }
public double roughing_offset { get; set; }
}
public class RootObject
{
public PartParameters part_parameters { get; set; }
}
public static void LoadJson()
{
using (System.IO.StreamReader r = new System.IO.StreamReader(@"C:\part_param.json"))
{
string json = r.ReadToEnd();
try
{
RootObject part = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(json);
_logger.Info(string.Format("list values : bardiameter: {0}, coreHeight: {1}, roughingOffset: {2}",
part.part_parameters.bar_diameter,part.part_parameters.core_height, part.part_parameters.roughing_offset));
}
catch (Exception e)
{
_logger.Info(string.Format("Read Json failed {0}", e.Message));
}
}
}
您必须在 属性 上方添加 JSON 属性,以便在反序列化对象时识别您的属性。
你会在下面找到一个例子
public class Parameters
{
[JsonProperty("bar_diameter")]
public float bar_diameter;
[JsonProperty("core_height")]
public float core_height;
[JsonProperty("roughing_offset")]
public float roughing_offset;
public Parameters(float barD, float coreH, float roughingO)
{
bar_diameter = barD;
core_height = coreH;
roughing_offset = roughingO;
}
}
我不断收到以下错误:
Object reference not set to an instance of an object
这是我在 C:\part_param.json
文件中的 Json 字符串{
"part_parameters" : {
"bar_diameter" : 300.4,
"core_height" : 132,
"roughing_offset" : 0.3
}
}
我使用的代码如下:
public class PLMpartParameter
{
public class Parameters
{
public float bar_diameter;
public float core_height;
public float roughing_offset;
public Parameters(float barD, float coreH, float roughingO)
{
bar_diameter = barD;
core_height = coreH;
roughing_offset = roughingO;
}
}
public Parameters parameters;
public PLMpartParameter(Parameters param)
{
parameters = param;
}
}
public static void LoadJson()
{
using (System.IO.StreamReader r = new System.IO.StreamReader(@"C:\part_param.json"))
{
string json = r.ReadToEnd();
_logger.Info(string.Format("Read entire file complete. File Values: {0}", json));
try
{
PLMpartParameter part = Newtonsoft.Json.JsonConvert.DeserializeObject<PLMpartParameter>(json);
}
catch (Exception e)
{
_logger.Info(string.Format("Read Json failed {0}", e.Message));
}
}
我在这里错过了什么?
我认为问题是你的 属性 被称为 'parameters' 但在你的 json 中它是 'part_parameters'.
您的代码存在一些问题:
- 您需要为 classes 使用默认构造函数(这是由于序列化程序如何处理类型 - 它们不会理解您的 class 特定的参数化构造函数)。
- 您的字段需要是可设置的属性(只需添加
{get;set;}
就可以了)。 - 我建议你用
[JsonProperty("part_parameters")]
修饰parameters
属性 以获得反序列化 您期望的行为。
引用 Prasad Telkikar 的回答立即修复
Use json2csharp to get model for your json file, then deserialize your json. You can use visual studio in build function to create class i.e. Edit -> Paste special -> Paste JSON as Class
Here is class
> public class PartParameters {
> public double bar_diameter { get; set; }
> public int core_height { get; set; }
> public double roughing_offset { get; set; } }
>
> public class RootObject {
> public PartParameters part_parameters { get; set; } }
要反序列化,请使用以下代码
PLMpartParameter part = Newtonsoft.Json.JsonConvert.DeserializeObject(json);
我的最终代码看起来像这样,并且可以正常工作!!
public class PartParameters
{
public double bar_diameter { get; set; }
public int core_height { get; set; }
public double roughing_offset { get; set; }
}
public class RootObject
{
public PartParameters part_parameters { get; set; }
}
public static void LoadJson()
{
using (System.IO.StreamReader r = new System.IO.StreamReader(@"C:\part_param.json"))
{
string json = r.ReadToEnd();
try
{
RootObject part = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(json);
_logger.Info(string.Format("list values : bardiameter: {0}, coreHeight: {1}, roughingOffset: {2}",
part.part_parameters.bar_diameter,part.part_parameters.core_height, part.part_parameters.roughing_offset));
}
catch (Exception e)
{
_logger.Info(string.Format("Read Json failed {0}", e.Message));
}
}
}
您必须在 属性 上方添加 JSON 属性,以便在反序列化对象时识别您的属性。
你会在下面找到一个例子
public class Parameters
{
[JsonProperty("bar_diameter")]
public float bar_diameter;
[JsonProperty("core_height")]
public float core_height;
[JsonProperty("roughing_offset")]
public float roughing_offset;
public Parameters(float barD, float coreH, float roughingO)
{
bar_diameter = barD;
core_height = coreH;
roughing_offset = roughingO;
}
}