gpt4 book ai didi

c# - 如何从 C# UNity 中写入的 JSON 格式的文件中删除特殊字符或不需要的符号?

转载 作者:太空宇宙 更新时间:2023-11-03 18:51:15 24 4
gpt4 key购买 nike

我写后读取时的JSON格式如下

�[{"SaveValues":[{"id":1,"allposition":{"x":-5.12429666519165,"y":4.792403697967529},"allrotation":{"x":0.0,"y":0.0,"z":0.0,"w":1.0},"allscale":{"x":1.0,"y":1.0},"linepos0":{"x":0.0,"y":0.0,"z":0.0},"linepos1":{"x":0.0,"y":0.0,"z":0.0},"movetype":1},{"id":1,"allposition":{"x":-4.788785934448242,"y":-3.4373996257781984},"allrotation":{"x":0.0,"y":0.0,"z":0.0,"w":1.0},"allscale":{"x":1.0,"y":1.0},"linepos0":{"x":0.0,"y":0.0,"z":0.0},"linepos1":{"x":0.0,"y":0.0,"z":0.0},"movetype":1}],"NoteValues":[{"movenumber":1,"notemsg":"Move One"}]},{"SaveValues":[{"id":1,"allposition":{"x":-5.12429666519165,"y":4.792403697967529},"allrotation":{"x":0.0,"y":0.0,"z":0.0,"w":1.0},"allscale":{"x":1.0,"y":1.0},"linepos0":{"x":0.0,"y":0.0,"z":0.0},"linepos1":{"x":0.0,"y":0.0,"z":0.0},"movetype":2},{"id":1,"allposition":{"x":-4.788785934448242,"y":-3.4373996257781984},"allrotation":{"x":0.0,"y":0.0,"z":0.0,"w":1.0},"allscale":{"x":1.0,"y":1.0},"linepos0":{"x":0.0,"y":0.0,"z":0.0},"linepos1":{"x":0.0,"y":0.0,"z":0.0},"movetype":2},{"id":2,"allposition":{"x":5.185188293457031,"y":4.803859233856201},"allrotation":{"x":0.0,"y":0.0,"z":0.0,"w":1.0},"allscale":{"x":1.0,"y":1.0},"linepos0":{"x":0.0,"y":0.0,"z":0.0},"linepos1":{"x":0.0,"y":0.0,"z":0.0},"movetype":2},{"id":2,"allposition":{"x":5.154441833496094,"y":-4.023111343383789},"allrotation":{"x":0.0,"y":0.0,"z":0.0,"w":1.0},"allscale":{"x":1.0,"y":1.0},"linepos0":{"x":0.0,"y":0.0,"z":0.0},"linepos1":{"x":0.0,"y":0.0,"z":0.0},"movetype":2}],"NoteValues":[{"movenumber":2,"notemsg":"Move Two"}]}]

下面给出了我用于保存为 JSON 格式的代码。

 ListContainer container = new ListContainer(getAlldata,playerNotes);

var temp = container;
//--Adding data in container into List<string> jsonstring
jsonstring.Add(JsonUtility.ToJson(temp));

//--Combing list of string into a single string
string jsons = "[" +string.Join(",", jsonstring)+"]";

//Writing into a JSON file in the persistent path
using (FileStream fs = new FileStream( Path.Combine(Application.persistentDataPath , savedName+".json"), FileMode.Create))
{
BinaryWriter filewriter = new BinaryWriter(fs);

filewriter.Write(jsons);
fs.Close();

}

在这里,我希望删除出现在 JSON 格式起点处的特殊字符。我正在尝试使用以下代码读取 JSON

 using (FileStream fs = new FileStream(Application.persistentDataPath + "/" + filename, FileMode.Open))
{


fs.Dispose();
string dataAsJson = File.ReadAllText(Path.Combine(Application.persistentDataPath, filename));
Debug.Log("DataJsonRead - - -" + dataAsJson);

}

我收到一个错误 - ArgumentException:JSON 解析错误:无效值。如何从一开始就删除那个特殊或不需要的符号?我认为这与将文件写入目录有关。在尝试使用其他方法保存时我没有找到任何字符或符号。

最佳答案

� 是 Unicode 替换字符,在尝试读取文本时发出,就好像它们是使用错误代码页用单字节代码页编码的一样。它不是 BOM - File.ReadAllText 会识别它并使用它使用正确的编码加载文件的其余部分。这意味着开头有垃圾。

问题是由于BinaryWriter使用不当造成的。该类用于将二进制格式的基本类型的字段写入流。对于字符串等可变长度类型,第一个字节包含字段长度。

这段代码:

using var ms=new MemoryStream();
using( BinaryWriter writer = new BinaryWriter(ms))
{
writer.Write(new String('0',3));
}

var b=ms.ToArray();

产生

3, 48,48,48

改用StreamWriterFile.WriteAllText。使用的默认编码是 UTF8,因此无需指定编码或尝试更改任何内容:

using (FileStream fs = new FileStream( Path.Combine(Application.persistentDataPath , savedName+".json"), FileMode.Create))
using(var writer=new StreamWriter(fs))
{
writer.Write(jsons);
}

var path=Path.Combine(Application.persistentDataPath , savedName+".json")
using(var writer=new StreamWriter(path))
{
writer.Write(jsons);
}

关于c# - 如何从 C# UNity 中写入的 JSON 格式的文件中删除特殊字符或不需要的符号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58452513/

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