首页 > 解决方案 > 如何在 Unity 中合并两个列表项并形成 JSON 数据?

问题描述

我正在寻找将两个列表中的项目转换为 JSON 数据。当我验证 JSON 时,它显示错误。下面给出的代码

public struct ListContainer
{

    public List<PlayerHandler> SaveValues;
    public List<PlayerMovement> NoteValues;


    public ListContainer(List<PlayerHandler> _saveVal,List<PlayerMovement> _noteVal)
    {
        SaveValues = _saveVal;
        NoteValues = _noteVal;

    }

}

//--Adding Two list into the container
ListContainer container = new ListContainer(getAlldata,playerNotes);

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

稍后我将上面的(CustomClass)jsonstring列表保存到 JSON 文件中。下面给出了将其保存到持久路径的编码。

public void Save()
{


    //--Get Text typed in the input box
    savedName = saveName.text;

    //--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(Application.persistentDataPath + "/" + savedName+".json" , FileMode.Create))
    {
        BinaryWriter filewriter = new BinaryWriter(fs);

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

    }

    saveButtonShow.SetActive(false);

}

当我从路径中检查 json 文件并尝试验证它是否显示下面给出的 error.JSON 时。

 {"SaveValues":[{"id":1,"allposition":{"x":-2.383237361907959,"y":-5.711871147155762},
"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":2,"allposition":{"x":-4.0806732177734379,"y":5.998472213745117},
"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":"First Move"}]},{"SaveValues":[{"id":1,"allposition":{"x":-2.383237361907959,"y":-5.711871147155762},
"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.558084964752197,"y":-5.517238140106201},
"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":-4.0806732177734379,"y":5.998472213745117},
"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":4.3838324546813969,"y":4.650305271148682},
"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":"Second Move"}]}

它显示的错误是

Error: Parse error on line 61:
...: "First Move"   }]}, {  "SaveValues": [
---------------------^
Expecting 'EOF', got ','

标签: c#jsonunity3d

解决方案


您使用的 JSON 验证器需要一个有效的 json 值,但您的文件包含两个用逗号分隔的 json 对象。

考虑通过用方括号将对象集合括起来将对象放入数组中。

此外,正如derHugo在评论中指出的那样,“而不是Application.persistentDataPath + "/" + savedName+".json"你应该总是选择Path.Combine(Application.persistentDataPath, savedName+".json")

public void Save()
{


    //--Get Text typed in the input box
    savedName = saveName.text;

    //--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();

    }

    saveButtonShow.SetActive(false);

}

这会产生这样的东西:

[{"SaveValues":[{"id":1,"allposition":{"x":-2.383237361907959,"y":-5.711871147155762},
"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":2,"allposition":{"x":-4.0806732177734379,"y":5.998472213745117},
"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":"First Move"}]},{"SaveValues":[{"id":1,"allposition":{"x":-2.383237361907959,"y":-5.711871147155762},
"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.558084964752197,"y":-5.517238140106201},
"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":-4.0806732177734379,"y":5.998472213745117},
"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":4.3838324546813969,"y":4.650305271148682},
"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":"Second Move"}]}]

推荐阅读