当前位置:网站首页>Unity发布webGL的时候JsonConvert.SerializeObject()转换失败
Unity发布webGL的时候JsonConvert.SerializeObject()转换失败
2022-06-25 08:01:00 【先生沉默先】
错误信息如图
火狐报错


谷歌报错


原因:webGL只能使用Unity自带的序列化工具
没有找到文档
修改方式
序列化字典Dictionary<TKey,TValue>
#region Dictionary序列化,
using System;
using System.Collections;
using System.Collections.Generic;
public class SerializeDictionary
{
public static string Dictionary2Json<TKey, TValue>(Dictionary<TKey, TValue> dic)
{
return JsonUtility.ToJson(new SerializeDictionary<TKey, TValue>(dic));
}
public static Dictionary<TKey, TValue> Json2Dictionary<TKey, TValue>(string str)
{
return JsonUtility.FromJson<SerializeDictionary<TKey, TValue>>(str).ToDictionary();
}
}
[Serializable]
public class SerializeDictionary<TKey, TValue> : ISerializationCallbackReceiver
{
[SerializeField]
List<TKey> keys;
[SerializeField]
List<TValue> values;
Dictionary<TKey, TValue> targetDictionary;
public Dictionary<TKey, TValue> ToDictionary() {
return targetDictionary; }
public SerializeDictionary(Dictionary<TKey, TValue> targetDictionary)
{
this.targetDictionary = targetDictionary;
}
public void OnBeforeSerialize()
{
keys = new List<TKey>(targetDictionary.Keys);
values = new List<TValue>(targetDictionary.Values);
}
public void OnAfterDeserialize()
{
var count = Math.Min(keys.Count, values.Count);
targetDictionary = new Dictionary<TKey, TValue>(count);
for (var i = 0; i < count; ++i)
{
targetDictionary.Add(keys[i], values[i]);
}
}
}
#endregion
使用方式
Dictionary<string, string> dc = new Dictionary<string, string>();
dc.Add("Helo","Hello");
dc.Add("Helo1", "Hello");
dc.Add("Helo2", "Hello");
var dicStr= SerializeDictionary.DicToJson(dc);
警告
转出的字符串并不是其他json可以直接反序列化出的字符串需要使用下面的方式重新反序列化
反序列化字典Dictionary
string requesDataStr="从上面转出来的json字符串";
Dictionary<string, List<string>> dicListList = new Dictionary<string, List<string>>();
dicListList = JsonConvert.DeserializeObject<Dictionary<string, List<string>>>(requesDataStr);
List<string> dicKeyList = dicListList["keys"];
List<string> dicValueList = dicListList["values"];
var count = Math.Min(dicKeyList.Count, dicKeyList.Count);
var targetdicc = new Dictionary<string, string>(count);
for (var i2 = 0; i2 < count; ++i2)
{
targetdicc.Add(dicKeyList[i2], dicValueList[i2]);
}
//targetdicc是从上面json转出来的Dictionary
序列化List
//是可以反序列化的
JsonConvert.DeserializeObject<T>(string);
// List<T>
[Serializable]
public class SerializationList<T>
{
[SerializeField]
List<T> targetList;
public List<T> ToList() { return targetList; }
public SerializationList(List<T> target)
{
this.targetList = target;
}
}
public class SerializeList
{
public static string List2Json<T>(List<T> l)
{
return JsonUtility.ToJson(new SerializationList<T>(l));
}
public static List<T> Json2List<T>(string str)
{
return JsonUtility.FromJson<SerializationList<T>>(str).ToList();
}
}
使用
// Start is called before the first frame update
void Start()
{
List<string> liststr = new List<string>();
liststr.Add("dsd");
liststr.Add("ds1d");
liststr.Add("d1sd");
string listInfo = SerializeList.List2Json(liststr);//序列化
Debug.Log(listInfo);
List<string> list2 = new List<string>();
List<string> c = SerializeList.Json2List<string>(listInfo);//反序列化,尖括号里面是那种类型的list就填那种类型就好。
}
不会可以评论私信Enjoy

。
边栏推荐
- About i/o -- the relationship between memory and CPU and disk
- 配置、软件配置项、软件配置管理项辨析
- What is the difference between TP5 and tp6?
- Check whether the point is within the polygon
- Various synchronous learning notes
- 【总结】1361- package.json 与 package-lock.json 的关系
- 《树莓派项目实战》第五节 使用Nokia 5110液晶屏显示Hello World
- Summary of NLP data enhancement methods
- 声纹技术(六):声纹技术的其他应用
- How to analyze the grey prediction model?
猜你喜欢

atguigu----17-生命周期

Fault: 0x800ccc1a error when outlook sends and receives mail

EasyPlayer流媒体播放器播放HLS视频,起播速度慢的技术优化

Prepare these before the interview. The offer is soft. The general will not fight unprepared battles

The city chain technology platform is realizing the real value Internet reconstruction!

【操作教程】TSINGSEE青犀视频平台如何将旧数据库导入到新数据库?

Summary of NLP data enhancement methods

About i/o -- the relationship between memory and CPU and disk

In Section 5 of bramble pie project practice, Nokia 5110 LCD is used to display Hello World

Word2vec, phrases, phraser, keyedvectors commonly used in gensim
随机推荐
故障:Outlook 收发邮件时的 0x800CCC1A 错误
声纹技术(二):音频信号处理基础
Lvs-dr mode multi segment case
对常用I/O模型进行比较说明
Retrieval model rough hnsw
How to implement a system call
SharePoint:SharePoint Server 2013 与 ADRMS 集成指南
SharePoint:SharePoint 2013 with SP1 简易安装
声纹技术(一):声纹技术的前世今生
Stimulsoft Ultimate呈现报告和仪表板
How to calculate the positive and negative ideal solution and the positive and negative ideal distance in TOPSIS method?
四、卷积神经网络(Convolution Neural Networks)
QSS 不同风格的按钮
[operation tutorial] how does the tsingsee Qingxi video platform import the old database into the new database?
Index analysis of DEMATEL model
华泰证券在上面开股票账户安全吗?
Sharepoint:sharepoint 2013 with SP1 easy installation
IC研发常用英文术语缩写
How to analyze the grey prediction model?
某视频网站m3u8非感知加密分析