Unity发布webGL的时候JsonConvert.SerializeObject()转换失败

错误信息如图

火狐报错

火狐报错
在这里插入图片描述

谷歌报错

谷歌报错
在这里插入图片描述

原因: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

在这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值