当前位置:网站首页>When unity released webgl, jsonconvert Serializeobject() conversion failed
When unity released webgl, jsonconvert Serializeobject() conversion failed
2022-06-25 08:50:00 【Sir, silence first】
The error message is shown in the figure
Firefox error reporting
Google error reporting
reason :webGL Only use Unity The serialization tool comes with
Document not found
Modify the way
Serialization Dictionary Dictionary<TKey,TValue>
#region Dictionary serialize ,
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
Usage mode
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);
Warning
The transferred out string is nothing else json The strings that can be deserialized directly need to be deserialized in the following way
Deserialize dictionary Dictionary
string requesDataStr=" From above json character string ";
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 It's from above json It turned out to be Dictionary
serialize List
// Can be deserialized
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();
}
}
Use
// 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);// serialize
Debug.Log(listInfo);
List<string> list2 = new List<string>();
List<string> c = SerializeList.Json2List<string>(listInfo);// Deserialization , Inside the angle brackets is the type of list Just fill in that type .
}
You can't comment on private letters Enjoy
.
边栏推荐
- 各种同步学习笔记
- mysql之Unknown table ‘COLUMN_STATISTICS‘ in information_schema (1109)
- Le labyrinthe des huit diagrammes de la bataille de cazy Chang'an
- [MySQL] understanding of transactions
- Exchange:管理日历权限
- Hyper-v:Hyper-v 第 1 代或第 2 代虚拟机
- 华泰证券在上面开股票账户安全吗?
- 【总结】1361- package.json 与 package-lock.json 的关系
- Find the nearest common ancestor (Sword finger offer) of two nodes in the binary tree (search tree)
- 获取扫码的客户端是微信还是支付宝
猜你喜欢
¥3000 | 录「TBtools」视频,交个朋友&拿现金奖!
Incluxdb time series database
C language "Recursion Series": recursively realizing the n-th power of X
自定义注解之编译时注解(RetentionPolicy.CLASS)
Data preprocessing: discrete feature coding method
【515. 在每个树行中找最大值】
微信小程序_7,项目练习,本地生活
检测点是否在多边形内
二、训练fashion_mnist数据集
Emergency administrative suspension order issued Juul can continue to sell electronic cigarette products in the United States for the time being
随机推荐
Exchange:管理日历权限
Incluxdb time series database
【OpenCV】—输入输出XML和YAML文件
买股票通过客户经理的开户二维码开户资金是否安全?想开户炒股
一、单个神经元网络构建
C language: bubble sort
Object. Can defineproperty also listen for array changes?
紧急行政中止令下达 Juul暂时可以继续在美国销售电子烟产品
Hyper-v:hyper-v generation 1 or generation 2 virtual machines
Advanced technology Er, meet internship position information
jmeter中csv参数化
进击的技术er,遇见实习岗位信息
How annotation lib and processor lib reference
声纹技术(四):声纹识别的工程部署
In Section 5 of bramble pie project practice, Nokia 5110 LCD is used to display Hello World
声纹技术(一):声纹技术的前世今生
第十五周作业
C#启动程序传递参数丢失双引号,如何解决?
nodejs 使用Express框架demo
《乔布斯传》英文原著重点词汇笔记(三)【 chapter one】