【问题标题】:How to combine two list items and form JSON data in Unity?如何在 Unity 中合并两个列表项并形成 JSON 数据?
【发布时间】: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&lt;ListContainer&gt;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/… 这是我添加前的问题 [.....]

标签: c# json unity3d


【解决方案1】:

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

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

此外,正如 derHugo 在 cmets 中指出的那样,“您应该始终选择 Path.Combine(Application.persistentDataPath, savedName+".json"),而不是 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"}]}]

【讨论】:

  • 请注意,您应该始终选择Path.Combine(Application.persistentDataPath, savedName+".json"),而不是Application.persistentDataPath + "/" + savedName+".json"
【解决方案2】:

即使你解决了对象坏字符串异常, 您必须将它们放入数组中,因为它们具有相同的值并且您正在重复键。

List<object> objList = new List<object>();
        objList.Add(list1);
        objList.Add(list2);

我为此创建代码示例

https://dotnetfiddle.net/KpZSiF

【讨论】:

    【解决方案3】:

    您应该将容器存储在容器列表中,而不是 json 中,并在写入磁盘之前进行序列化。

    var containers = new List<ListContainer>();
    
    (...)
    
    ListContainer container = new ListContainer(getAlldata,playerNotes);
    containers.Add(container);
    
    (...)
    
    string json = JsonUtility.ToJson(containers);
    // maybe even = JsonUtility.ToJson( new { Players = containers } ); but that's not important.
    
    // Save to file, your way or 
    System.IO.File.WriteAllText(path, json);
    

    【讨论】:

      猜你喜欢
      • 2018-10-25
      • 1970-01-01
      • 2020-10-16
      • 1970-01-01
      • 2015-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-05
      相关资源
      最近更新 更多