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

标签 c# json unity3d

我写后读取时的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/

相关文章:

c# - Linq 查询返回错误

java - 一个接受 JSON 对象的简单 JaxWS Rest

c# - unity2d如何让场景在后台运行

c# - 让球跳跃

c# - 在 xamarin 表单中获取位置

c# - Entity Framework 中同一类的多个列表

c# - 等待 MoveTo 操作完成的正确方法是什么?

javascript - 无法从 .json 文件加载 json 数据

ios - Swift 使用 String 创建 JSON 对象

c# - 二维物体碰撞统一