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

zyo*_*neo 1 c# json unity-game-engine

编写后阅读时的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"}]}]
Run Code Online (Sandbox Code Playgroud)

下面提供了我用于保存为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();

    }
Run Code Online (Sandbox Code Playgroud)

在这里,我希望删除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);

        }
Run Code Online (Sandbox Code Playgroud)

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

Pan*_*vos 7

? is the Unicode Replacement character, emitted when there's an attempt to read text as if they were encoded with a single-byte codepage using the wrong codepage. It's not a BOM - File.ReadAllText would recognize it and use it to load the rest of the file using the correct encoding. This means there's garbage at the start.

The problem is caused by the inappropriate use of BinaryWriter. That class is used to write fields of primitive types in a binary format to a stream. For variable length types like stings, the first byte(s) contain the field length.

This code :

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

var b=ms.ToArray();
Run Code Online (Sandbox Code Playgroud)

Produces

3, 48,48,48
Run Code Online (Sandbox Code Playgroud)

Use StreamWriter or File.WriteAllText instead. The default encoding used is UTF8 so there's no need to specify an encoding or try to change anything :

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

or

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

Run Code Online (Sandbox Code Playgroud)