gpt4 book ai didi

c# - 使用 JsonFX 反序列化字典

转载 作者:太空宇宙 更新时间:2023-11-03 10:52:03 25 4
gpt4 key购买 nike

我正在尝试序列化 Dictionary<string, object> 类型的字典存储一系列参数。该字典包含原始变量类型和复杂变量类型(例如列表)。然而,当将 JSON 字符串反序列化为 Dictionary<string, object> 时,序列化按预期工作。 ,那些类型为 List<T> 的参数转换为类型 Dictionary<string, object> .当我尝试对这些参数进行类型转换时,我得到一个 InvalidCastException .

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using JsonFx.Json;

public class LevelBuilderStub : MonoBehaviour
{
class Person
{
public string name;
public string surname;
}

// Use this for initialization
void Start ()
{
Dictionary<string, object> parameters = new Dictionary<string, object>();
List<Person> persons = new List<Person>();
persons.Add(new Person() { name = "Clayton", surname = "Curmi" });
persons.Add(new Person() { name = "Karen", surname = "Attard" });

parameters.Add("parameterOne", 3f);
parameters.Add("parameterTwo", "Parameter string info");
parameters.Add("parameterThree", persons.ToArray());

string json = JsonWriter.Serialize(parameters);
AVDebug.Log(json);

parameters = null;
parameters = JsonReader.Deserialize(json, typeof(Dictionary<string, object>)) as Dictionary<string, object>;

foreach(KeyValuePair<string, object> kvp in parameters)
{
string key = kvp.Key;
object val = kvp.Value;
AVDebug.Log(string.Format("Key : {0}, Value : {1}, Type : {2}", key, val, val.GetType()));
}
}
}

这将返回以下内容;

{"parameterOne":3,"parameterTwo":"Parameter string info","parameterThree":[{"name":"Clayton","surname":"Curmi"},{"name":"Karen","surname":"Attard"}]}
Key : parameterOne, Value : 3, Type : System.Int32
Key : parameterTwo, Value : Parameter string info, Type : System.String
Key : parameterThree, Value : System.Collections.Generic.Dictionary`2[System.String,System.Object][], Type : System.Collections.Generic.Dictionary`2[System.String,System.Object][]

问题是,我怎样才能得到 List<Person>对于参数键“parameterThree”。请注意,参数字典的内容将根据其上下文而有所不同。

最佳答案

找到解决方案!必须使用 JsonName 属性标记正在序列化的类,然后使用写入器/读取器设置在 JSON 输出中包含变量的程序集名称。以前面的例子为例,这里是你必须做的;

using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using JsonFx.Json;

[Serializable]
[JsonName("Person")]
public class Person
{
public string name;
public string surname;
}

[JsonName("Animal")]
public class Animal
{
public string name;
public string species;
}

[Serializable]
public class Parameters
{
public float floatValue;
public string stringValue;
public List<Person> listValue;
}

public class SerializationTest : MonoBehaviour
{
// Use this for initialization
void Start()
{
ScenarioOne();
}

void ScenarioOne()
{
Dictionary<string, object> parameters = new Dictionary<string, object>();
List<Person> persons = new List<Person>();
persons.Add(new Person() { name = "Clayton", surname = "Curmi" });
persons.Add(new Person() { name = "Karen", surname = "Attard" });

List<Animal> animals = new List<Animal>();
animals.Add(new Animal() { name = "Chimpanzee", species = "Pan troglodytes" });
animals.Add(new Animal() { name = "Cat", species = "Felis catus" });

parameters.Add("floatValue", 3f);
parameters.Add("stringValue", "Parameter string info");
parameters.Add("persons", persons.ToArray());
parameters.Add("animals", animals.ToArray());

// ---- SERIALIZATION ----

JsonWriterSettings writerSettings = new JsonWriterSettings();
writerSettings.TypeHintName = "__type";

StringBuilder json = new StringBuilder();
JsonWriter writer = new JsonWriter(json, writerSettings);
writer.Write(parameters);

AVDebug.Log(json.ToString());

// ---- DESERIALIZATION ----

JsonReaderSettings readerSettings = new JsonReaderSettings();
readerSettings.TypeHintName = "__type";

JsonReader reader = new JsonReader(json.ToString(), readerSettings);

parameters = null;
parameters = (Dictionary<string, object>)reader.Deserialize();

foreach (KeyValuePair<string, object> kvp in parameters)
{
string key = kvp.Key;
object val = kvp.Value;
AVDebug.Log(val == null);
AVDebug.Log(string.Format("Key : {0}, Value : {1}, Type : {2}", key, val, val.GetType()));
}
}
}

关于c# - 使用 JsonFX 反序列化字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21092870/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com