JSON 作者不包含 类 从 ObservableCollection 派生的属性
JSON writer doesn't include properties on classes derived from ObservableCollection
我有以下 类:
public class Goal : ObservableCollection<Activity>
{
public string Name { get; set; }
// constructors
public Goal() { }
public Goal(string name)
{
Name = name;
}
}
public class Activity
{
// properties
public string Name { get; set; }
public string Details { get; set; }
public bool FilterMe { get; set; }
// constructors
public Activity() { }
public Activity(string name)
{
Name = name;
}
}
当我将其写成 JSON 时,具有所有 public Activity 属性的 Activity 列表被正确输出,但是名称 属性目标不包括在内。我做错了什么?
// create a goal
Goal goal = new("Goal 1");
for (int a = 0; a < 5; a++)
{
Activity activity = new($"Activity {a + 1}");
if (a % 2 == 0) { activity.FilterMe = true; }
goal.Add(activity);
}
// write the output file
using FileStream fsWrite = File.Create("C:\Users\me\Desktop\Test.json");
JsonSerializer.Serialize<Goal>(new Utf8JsonWriter(fsWrite, new JsonWriterOptions() { Indented = true }), goal, new JsonSerializerOptions() { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, IgnoreReadOnlyProperties = true });
这是示例输出:
{
"Goal": [
{
"Name": "Activity 1",
"Details": "",
"FilterMe": true
},
{
"Name": "Activity 2",
"Details": "",
"FilterMe": false
},
{
"Name": "Activity 3",
"Details": "",
"FilterMe": true
},
{
"Name": "Activity 4",
"Details": "",
"FilterMe": false
},
{
"Name": "Activity 5",
"Details": "",
"FilterMe": true
}
]
}
如您在上面的输出中所见,缺少目标的名称 属性。这是一个 public 属性,所以我假设序列化程序会拾取它。
它永远不会按照您想要的方式工作,这不是序列化程序问题,而是 C# 创建对象的方式。即使您尝试使用文本编辑器以您想要的方式手动创建 json,它也将是无效的 json。您将无法使用它。所以我唯一可以推荐给你的,就是把你的class改成这个
public class Goal
{
public ObservableCollection<Activity> Goals {get; set;} = new();
public string Name { get; set; }
public void Add (Activity activity)
{
Goals.Add(activity);
}
// constructors
public Goal() { }
public Goal(string name)
{
Name = name;
}
}
并且您的所有代码都可以正常工作
PS.
我非常有兴趣在连载后看到您想要的 json,请修复 json 您手动发布的方式并向我们展示。
我有以下 类:
public class Goal : ObservableCollection<Activity>
{
public string Name { get; set; }
// constructors
public Goal() { }
public Goal(string name)
{
Name = name;
}
}
public class Activity
{
// properties
public string Name { get; set; }
public string Details { get; set; }
public bool FilterMe { get; set; }
// constructors
public Activity() { }
public Activity(string name)
{
Name = name;
}
}
当我将其写成 JSON 时,具有所有 public Activity 属性的 Activity 列表被正确输出,但是名称 属性目标不包括在内。我做错了什么?
// create a goal
Goal goal = new("Goal 1");
for (int a = 0; a < 5; a++)
{
Activity activity = new($"Activity {a + 1}");
if (a % 2 == 0) { activity.FilterMe = true; }
goal.Add(activity);
}
// write the output file
using FileStream fsWrite = File.Create("C:\Users\me\Desktop\Test.json");
JsonSerializer.Serialize<Goal>(new Utf8JsonWriter(fsWrite, new JsonWriterOptions() { Indented = true }), goal, new JsonSerializerOptions() { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, IgnoreReadOnlyProperties = true });
这是示例输出:
{
"Goal": [
{
"Name": "Activity 1",
"Details": "",
"FilterMe": true
},
{
"Name": "Activity 2",
"Details": "",
"FilterMe": false
},
{
"Name": "Activity 3",
"Details": "",
"FilterMe": true
},
{
"Name": "Activity 4",
"Details": "",
"FilterMe": false
},
{
"Name": "Activity 5",
"Details": "",
"FilterMe": true
}
]
}
如您在上面的输出中所见,缺少目标的名称 属性。这是一个 public 属性,所以我假设序列化程序会拾取它。
它永远不会按照您想要的方式工作,这不是序列化程序问题,而是 C# 创建对象的方式。即使您尝试使用文本编辑器以您想要的方式手动创建 json,它也将是无效的 json。您将无法使用它。所以我唯一可以推荐给你的,就是把你的class改成这个
public class Goal
{
public ObservableCollection<Activity> Goals {get; set;} = new();
public string Name { get; set; }
public void Add (Activity activity)
{
Goals.Add(activity);
}
// constructors
public Goal() { }
public Goal(string name)
{
Name = name;
}
}
并且您的所有代码都可以正常工作
PS.
我非常有兴趣在连载后看到您想要的 json,请修复 json 您手动发布的方式并向我们展示。