【发布时间】:2019-10-17 06:00:29
【问题描述】:
我希望将两个列表中的项目转换为 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 在那里结束......你有一个
List<ListContainer>或ListContainer[]那里......所以它应该被[ ... ]包围 -
但我仍然无法从中获取值....我尝试了 Debug.Log("Save value count = " + JNode[0]["NoteValues"]["notemsg"]);获得“第一步”输出
-
你试过
JNode[1]了吗?这也取决于我猜的库...根据错误消息,您似乎正在使用jsoneditoronline.org(使用jsonlint)之类的东西进行测试...也许您在c#中使用的解析库了解您的给定 JSON 更好......另请参阅 Ruzihm's answer 他已经在 c# 中发布了一个可能的解决方案 -
stackoverflow.com/questions/58407055/… 这是我添加前的问题 [.....]