c# - 如何在 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 ','
解决方案
您使用的 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"}]}]
推荐阅读
- javascript - 调用导入类的方法时使用 Async/Await
- sql - 嵌套 XML 输出
- html - Angular 6 - 在 mat-select 上动态设置 [必需]
- asp.net-mvc - 模型的类属性的属性不具有约束力
- botframework - 自动化微软机器人测试
- docker - 同一个 docker 端口可以用于两个不同的应用程序吗?
- ibm-mq - 我们可以用其他用户重命名 MUSR_MQADMIN
- javascript - Bootstrap-vue - 如何以编程方式显示/隐藏 b 表列
- sql - 在选择字段中使用保留字
- python - PyFPDF返回空白页 - Python 2.7