读取Json错误,JSON parse error: The document root must not follow by other values.

报错截图

ArgumentException: JSON parse error: The document root must not follow by other values.
UnityEngine.JsonUtility.FromJson (System.String json, System.Type type) (at <588829356379400e8d4015563af46ed0>:0)
UnityEngine.JsonUtility.FromJson[T] (System.String json) (at <588829356379400e8d4015563af46ed0>:0)
JsonTools.JsonToObject[T] (System.String fileFullPath) (at Assets/Script/Tools/JsonTools.cs:10)
DebugAssetDatabase.B () (at Assets/Script/Test/Editor/DebugAssetDatabase.cs:80)

 

原因

JSON文本 格式不正确,在线校验检查后发现多大括号。

虽然不知道 这个括号如何出现的,json 文件为自动生成。

 

{
    "config": {
        "url": "asdlkjfla",
        "updateVersion": "xxxasdlkj",
        "path": "asdklfj",
        "des": "xx"
    },
    "sumSize": 0.0,
    "updateSize": 142.0,
    "bundleInfos": [
        {
            "Url": "",
            "Path": "",
            "MD5": "xxxalksdj",
            "Crc32": 0,
            "ABName": "xx",
            "Size": 0.0,
            "ABDependce": []
        },
        {
            "Url": "",
            "Path": "",
            "MD5": "xxxalksdj",
            "Crc32": 0,
            "ABName": "xx",
            "Size": 0.0,
            "ABDependce": []
        }
    ]
    }
}

JSON文件格式不对原因

2020年5月13日09点47分

查出来生成的原因,文件写入导致的问题。

public static void WriteJson(object obj,string fileFullPath)
{
        string content = JsonUtility.ToJson(obj, true);

        Debug.Log(content);
        string Dir = Path.GetDirectoryName(fileFullPath);

        if (Directory.Exists(Dir) == false)
        {
            Directory.CreateDirectory(Dir);
        }

        Stream stream = File.Open(fileFullPath, FileMode.OpenOrCreate, FileAccess.Write);
        StreamWriter sw = new StreamWriter(stream, new UTF8Encoding(false));
        sw.Write(content);
        sw.Close();
}

 

 

关键点在FileMode上,文件模式。

OpenOrCreate 是当前有文件时,从头开始写入数据,有多少写多少,而不是覆盖。

改成FileMode.Truncate 即可

 

点赞
  1. viagra buy说道:
    Google Chrome Windows 10

    A lot of thanks for all your efforts on this site. Gloria enjoys doing investigation and it's easy to understand why. Almost all know all concerning the dynamic means you deliver great items on the web blog and as well encourage contribution from other people on that concern plus our daughter is actually being taught a lot. Have fun with the remaining portion of the new year. You're the one conducting a glorious job.

发表评论

昵称和uid可以选填一个,填邮箱必填(留言回复后将会发邮件给你)
tips:输入uid可以快速获得你的昵称和头像

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据

Title - Artist
0:00