当前位置:   article > 正文

Unity 加载UTF-8 *.Json文件,调用JsonMapper.ToObject异常

Unity 加载UTF-8 *.Json文件,调用JsonMapper.ToObject异常

*.Json文件使用UTF-8文件不保存格式的话被LitJson 中的JsonMapper.ToObject 调用报异常:

JsonException: Invalid character '' in input string

LitJson.Lexer.NextToken ()
LitJson.JsonReader.ReadToken ()
LitJson.JsonReader.Read ()
LitJson.JsonMapper.ReadValue (LitJson.WrapperFactory factory, LitJson.JsonReader reader)
LitJson.JsonMapper.ToWrapper (LitJson.WrapperFactory factory, System.String json)

LitJson.JsonMapper.ToObject (System.String json)


有用UTF-8保存文件有4个无用字节i.e.  


  1. using UnityEngine;
  2. using System.Collections;
  3. using LitJson;
  4. using System.Text;
  5. public class GetVersion : MonoBehaviour {
  6. string CallFun = "";
  7. string filepath = "";
  8. private string _tipMessage = "";
  9. [SerializeField]
  10. private UISprite _loginBt;
  11. private Color _color=new Color(1,1,1,0.5f);
  12. void Start () {
  13. InitWWWJson("file://"+Application.dataPath + "/StreamingAssets/MusesMobileAppVersion.json", "");
  14. }
  15. public void InitWWWJson(string Path, string CallFun)
  16. {
  17. this.CallFun = CallFun;
  18. filepath = Path;
  19. WWW www = new WWW(filepath);
  20. StartCoroutine(LinkNetWork(www));
  21. }
  22. IEnumerator LinkNetWork(WWW www)
  23. {
  24. yield return www;
  25. if (www.error == null)
  26. {
  27. //ReadJson(www.text);直接使用抛异常
  28. string jsonData = System.Text.Encoding.UTF8.GetString(www.bytes, 3, www.bytes.Length - 3); // Skip thr first 3 bytes (i.e. the UTF8 BOM)
  29. ReadJson(jsonData);
  30. }
  31. }
  32. private void ReadJson(string versionMessage)
  33. {
  34. Debug.Log( versionMessage);
  35. JsonData _json = JsonMapper.ToObject(versionMessage)["Diet"];
  36. string version= _json["version"].ToString();
  37. Debug.Log("版本号" + version);
  38. Debug.Log("name:" + _json["name"].ToString());
  39. if (version != Application.version)
  40. {
  41. _tipMessage = _json["msg"].ToString();
  42. Messenger.Broadcast<string>("MessageBoxError", _tipMessage);
  43. if (_json["enabled"].ToString() != "true")
  44. {
  45. _loginBt.color = _color;
  46. _loginBt.transform.GetComponent<BoxCollider>().enabled = false;
  47. }
  48. }
  49. }
  50. }


JSON文件

{
    "Diet": {
        "name": "91小二",
        "version": "1.1.2",
	"enabled":"true",
	"msg":"检测到新版本,请更新",
        "updateTime": "20160628"
    }
}
这个是一个简单的检测版本是否需要更新并提升,把*.json放到服务器上就可以了



声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/123416
推荐阅读
相关标签
  

闽ICP备14008679号