c# - 无法将 FileStream 隐式转换为 Json 文件的 StreamWriter
问题描述
所以,我正在阅读一本关于 .NET Core 3 的书,并且我正在学习如何序列化 JSON 文件的部分。我遇到了我无法找到解决方案的类型的问题。我尝试过解决它,但后来我遇到了其他类型的问题。我在下面的代码中遇到的问题是 Main at 的最后一个 using 语句中的 CS0029 File.Create(jsonPath)
:
无法将类型“System.IO.FileStream”隐式转换为“System.IO.StreamWriter”
根据我从 Microsoft 文档中获得的示例,我尝试将 using 语句更改为使用 FileStream 类型而不是 StreamWriter,但这会在 jsonStream 变量的jsonSerializer.Serialize(jsonStream, people)
行中产生错误:
无法从“System.IO.FileStream”转换为“System.IO.TextWriter”
下面是代码:
using System;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.IO;
using Packt.Shared;
using static System.Console;
using static System.Environment;
using static System.IO.Path;
namespace WorkingWithSerialization
{
class Program
{
static void Main(string[] args)
{
List<Person> people = new List<Person>
{
new Person(30000M)
{
FirstName = "Adam",
LastName = "Smith",
DateOfBirth = new DateTime(1756,12,12)
},
new Person(40000M)
{
FirstName = "Lisa",
LastName = "Simpson",
DateOfBirth = new DateTime(1980,4,30)
},
new Person(20000M)
{
FirstName = "Barney",
LastName = "Rubble",
DateOfBirth = new DateTime(1962,7,14),
Children = new HashSet<Person>
{
new Person(0M)
{
FirstName = "Bob",
LastName = "Rubble",
DateOfBirth = new DateTime(1986,5,3)
},
new Person(0M)
{
FirstName = "Bamm-Bamm",
LastName = "Rubble",
DateOfBirth = new DateTime(1984,3,15)
}
}
}
};
XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<Person>));
string path = Combine(CurrentDirectory, "people.xml");
using(FileStream stream = File.Create(path))
{
xmlSerializer.Serialize(stream,people);
}
WriteLine($"Wrote {new FileInfo(path).Length} bytes to {path}.");
WriteLine($"\n{File.ReadAllText(path)}");
using(FileStream xmlLoad = File.Open(path, FileMode.Open))
{
List<Person> loadedPeople = (List<Person>)xmlSerializer.Deserialize(xmlLoad);
foreach(Person person in loadedPeople)
{
WriteLine($"{person.FirstName} has {person.Children.Count} children.");
}
}
string jsonPath = Combine(CurrentDirectory, "people.json");
using(StreamWriter jsonStream = File.Create(jsonPath))
{
var jsonSerializer = new Newtonsoft.Json.JsonSerializer();
jsonSerializer.Serialize(jsonStream, people);
}
}
}
}
我还在 .csproj 文件中包含了最新版本的 Newtonsoft.Json,但我认为这不是问题所在。
我也尝试了不同的铸造方式,但没有积极的结果。我不确定我做错了什么。
解决方案
您需要一个额外的步骤来创建一个StreamWriter
使用FileStream
.
string jsonPath = Combine(CurrentDirectory, "people.json");
using (FileStream fs = File.Create(jsonPath))
using (StreamWriter jsonStream = new StreamWriter(fs))
{
var jsonSerializer = new Newtonsoft.Json.JsonSerializer();
jsonSerializer.Serialize(jsonStream, people);
}
如果我创建一个最小的 Person 类以便能够编译您的代码,我会得到一个如下所示的 json 文件:
[{
"FirstName": "Adam",
"LastName": "Smith",
"DateOfBirth": "1756-12-12T00:00:00",
"Children": null
}, {
"FirstName": "Lisa",
"LastName": "Simpson",
"DateOfBirth": "1980-04-30T00:00:00",
"Children": null
}, {
"FirstName": "Barney",
"LastName": "Rubble",
"DateOfBirth": "1962-07-14T00:00:00",
"Children": [{
"FirstName": "Bob",
"LastName": "Rubble",
"DateOfBirth": "1986-05-03T00:00:00",
"Children": null
}, {
"FirstName": "Bamm-Bamm",
"LastName": "Rubble",
"DateOfBirth": "1984-03-15T00:00:00",
"Children": null
}
]
}
]
推荐阅读
- c# - 无法将特殊字符转换为 UTF-8
- python - 在 Python 中通过参数名称获取 argparse 帮助消息
- android - Kotlin 协程流程在继续运行时返回第一个结果
- forms - PowerShell GUI - 表单中的函数不起作用
- java - Android studio 如何找到所有可用的蓝牙设备
- aws-lambda - AWS SQS 在 DLQ 中重新传递消息
- javascript - 如何在数组中求和或计算值使用vue
- php - 如何使用 php 中的 addFromString 函数创建包含 PDF 文件的 zip 文件?
- meshlab - 使用 Quadro RTX 6000 显卡的 Meshlab 上的可视化问题
- php - Woocommerce - 如何在管理订单页面中更改小计项目的值