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

 Firefox error reporting
 Insert picture description here

Google error reporting

 Google error reporting
 Insert picture description here

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

 Insert picture description here

.

原网站

版权声明
本文为[Sir, silence first]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206250801478566.html