当前位置:网站首页>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

。
边栏推荐
- 《树莓派项目实战》第五节 使用Nokia 5110液晶屏显示Hello World
- Is it really safe to pay new debts? Is it risky
- A 35 year old Tencent employee was laid off and sighed: a suite in Beijing, with a deposit of more than 7 million, was anxious about unemployment
- 初识生成对抗网络(11)——利用Pytorch搭建WGAN生成手写数字
- About i/o -- the relationship between memory and CPU and disk
- Le labyrinthe des huit diagrammes de la bataille de cazy Chang'an
- 【MYSQL】事务的理解
- Easyplayer streaming media player plays HLS video. Technical optimization of slow starting speed
- InfluxDB时序数据库
- 声纹技术(五):声纹分割聚类技术
猜你喜欢

City Chain technology platform, really Realizing value Internet reconstruction!

wav文件(波形文件)格式分析与详解

How to solve the 10061 error of MySQL in Linux

Meaning of Jieba participle part of speech tagging

Lvs-dr mode multi segment case

C language: count the number of characters, numbers and spaces

在二叉树(搜索树)中找到两个节点的最近公共祖先(剑指offer)

How to calculate the correlation coefficient and correlation degree in grey correlation analysis?

Various synchronous learning notes

【操作教程】TSINGSEE青犀视频平台如何将旧数据库导入到新数据库?
随机推荐
声纹技术(五):声纹分割聚类技术
第十五周作业
城鏈科技平臺,正在實現真正意義上的價值互聯網重構!
配置、软件配置项、软件配置管理项辨析
UEFI:修复 EFI/GPT Bootloader
Is it safe to open a stock account online now?
OpenFOAM:底层
RTOS 多线程下hardfault问题总结
【操作教程】TSINGSEE青犀视频平台如何将旧数据库导入到新数据库?
Hyper-v:hyper-v generation 1 or generation 2 virtual machines
GPU calculation
声纹技术(四):声纹识别的工程部署
某视频网站m3u8非感知加密分析
EasyPlayer流媒体播放器播放HLS视频,起播速度慢的技术优化
What are the indicators of VIKOR compromise?
Iframe is simple to use, iframe is obtained, iframe element value is obtained, and iframe information of parent page is obtained
What about the exponential smoothing index?
打新债安全性有多高啊
What is the file that tp6 automatically executes? What does the tp6 core class library do?
Sharepoint:sharepoint 2013 with SP1 easy installation