0

我只是想序列化一些数据并反序列化回来,它必须在iOS平台上工作。Json.NET 不适用于 iOS。JsonFX 可以序列化数据,字符串看起来像 Json.Net 序列化的字符串,但 JsonFX 不能反序列化数据。这是简单的代码,它给了我下一个错误

InvalidCastException:无法从源类型转换为目标类型。

using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
using Pathfinding.Serialization.JsonFx;

public class jsonTest : MonoBehaviour {

    public Text text;
    public List<ChapterModel> chapters;

    // Use this for initialization
    void Start () {
         chapters = new List<ChapterModel>();

         for(int i=0; i<2; i++)
         {
             var cm = new ChapterModel();
             cm.pages = new List<PageModel>();
             cm.id = i;

             for(int j =0; j<2; j++)
             {
                 var pm = new PageModel();
                 pm.BackgroundArt = j.ToString();
                 cm.pages.Add(pm);
             }

             chapters.Add(cm);
         }

        Debug.Log(JsonReader.Deserialize<List<ChapterModel>>(JsonWriter.Serialize(chapters)).Count);
    }
}

有谁知道如何解决它?

4

1 回答 1

1

在玩了一点 JsonFX 之后,我发现它使用起来非常棘手:

using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
using System.Linq;
using Pathfinding.Serialization.JsonFx;
using UnityEditor;

public class jsonTest : MonoBehaviour
{

public Text text;
public List<ChapterModel> chapters;

// Use this for initialization
void Start()
{
    chapters = new List<ChapterModel>();

    for (int i = 0; i < 2; i++)
    {
        var cm = new ChapterModel();
        cm.pages = new List<PageModel>();
        cm.id = i;

        for (int j = 0; j < 2; j++)
        {
            var pm = new PageModel();
            pm.BackgroundArt = j.ToString();
            cm.pages.Add(pm);
        }

        chapters.Add(cm);
    }

    string json = JsonWriter.Serialize(chapters);

    //Will end up in in invalid cast:
    //var deserialized = JsonReader.Deserialize<List<ChapterModel>(json);

    //Will be casted to object[] on deserialization:
    //var deserialized = JsonReader.Deserialize(json, typeof (List<ChapterModel>));

    //Will evaluate just fine:
    var deserialized = JsonReader.Deserialize(json, typeof(List<ChapterModel>)) as object[];
    List<ChapterModel> list = deserialized.Select(item => item as ChapterModel).ToList();
    Debug.Log(list.Count);
    }
}

[System.Serializable]
public class ChapterModel
{
   public int id = -1;
   public List<PageModel> pages;
}


[System.Serializable]
public class PageModel
{
   public string BackgroundArt,
    ForeAddedArt,
    VFXloop, VFXappeared, VFXnextSlide,
    BGAmbientLoop, BGMusicLoop, BGVoiceLoop,
    VFXSound, MusicSound, VoiceSound;
}

但是,如果我们让 JSON 知道 a typehint,应该没问题。有趣的是,它不是:

    //(...)
    StringBuilder output = new StringBuilder();

    JsonWriterSettings writerSettings = new JsonWriterSettings { TypeHintName = "__type" };

    JsonWriter writer = new JsonWriter(output, writerSettings);
    writer.Write(chapters);
    JsonReaderSettings readerSettings = new JsonReaderSettings { TypeHintName = "__type" };
    JsonReader reader = new JsonReader(output, readerSettings);

    //Will now see objects inside array as ChapterModel but still will fail to cast properly to list/array of ChapterModels
    List<ChapterModel> deserialized = reader.Deserialize() as List<ChapterModel>;
    Debug.Log(deserialized.Count);

你可能想多玩一点,或者坚持我上面提供的丑陋版本。

于 2015-11-29T02:23:37.167 回答