我在文档或相关问题中找不到任何内容。
我正在尝试反序列化以下json文件( tile_config.json ):
{
"tile_info": [
{
"height": 32,
"width": 32
}
],
"tiles-new": [
{
"file": "tiles.png",
"tiles": [
{
"id": "t_dirt",
"fg": [
{ "weight":500, "sprite":640},
{ "weight":1, "sprite":3620},
{ "weight":1, "sprite":3621},
{ "weight":1, "sprite":3622}
],
"rotates": false
},
{
"id": "t_fence_rope",
"fg": 2016,
"bg": 640
}
]
}
]
}
键“fg”可以是整数或对象数组,如示例中所示。
我尝试使用以下代码进行反序列化:
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
namespace JSONTest
{
class Program
{
static void Main(string[] args)
{
load_json();
}
public static void load_json()
{
var ser = new DataContractJsonSerializer(typeof(TileConfig));
System.IO.FileStream file =
new System.IO.FileStream(".\\tile_config.json", System.IO.FileMode.Open);
var tileconfig = (TileConfig)ser.ReadObject(file);
file.Close();
var Tile0 = tileconfig.tiles_new[0].tiles[0];
Console.WriteLine(Tile0.id);
//Output: t_dirt
Console.WriteLine(Tile0.fg);
//Output: System.Object[]
Console.WriteLine(((Object[])Tile0.fg)[0]);
//Output: System.Object
//This doesn't work => InvalidCastException
// var try1 = (JSON_Loader.TileConfig.TileValues.TileIndex)((Object[])tileconfig.tiles_new[0].tiles[0].fg)[0];
//This doesn't work either => InvalidCastException
//var try2 = ((Dictionary<object,object>)tileconfig.tiles_new[0].tiles[0].fg);
//var try3 = ((Dictionary<string, object>)tileconfig.tiles_new[0].tiles[0].fg);
//For the second one this works
var Tile1 = tileconfig.tiles_new[0].tiles[1];
Console.WriteLine(Tile1.id);
//Output: t_fence_rope
Console.WriteLine(Tile1.fg);
//Output: 2016
var try4 = ((int)Tile1.fg);
Console.WriteLine(try4);
//Output: 2016
Console.ReadLine();
}
}
[DataContract]
public class TileConfig
{
[DataMember(Order = 0)]
public List<TileInfo> tile_info;
[DataMember(Name = "tiles-new", Order = 1)]
public List<TileValues> tiles_new;
[DataContract]
public class TileInfo
{
[DataMember(EmitDefaultValue = false, Order = 0)]
public int height;
[DataMember(EmitDefaultValue = false, Order = 1)]
public int width;
}
[DataContract]
public class TileValues
{
[DataMember(EmitDefaultValue = false, Order = 0)]
public string file;
[DataMember(EmitDefaultValue = false, Order = 5)]
public Tile[] tiles;
[DataContract]
public class Tile
{
[DataMember(EmitDefaultValue = false, Order = 0)]
public string id;
[DataMember(EmitDefaultValue = false, Order = 1)]
public Object fg;
[DataMember(EmitDefaultValue = false, Order = 2)]
public int bg;
[DataMember(EmitDefaultValue = false, Order = 3)]
bool rotates;
}
[DataContract]
public class TileIndex
{
[DataMember(EmitDefaultValue = false, Order = 0)]
public int weight;
[DataMember(EmitDefaultValue = false, Order = 1)]
public int[] sprite;
}
}
}
}
正如您所看到的,我试图通过将fg声明为Object来解决问题。这部分起作用。该文件反序列化没问题。但我无法获得“fg”的数据(我还没发现)。我尝试将它转换为字典,将其转换为TileIndex(因为我会猜到它是什么)并且我试图将其转换为字符串,但这不起作用或者我不知道如何使其工作在这种情况下。
现在我猜测fg被设置为某个东西,因为它不是null而ToString正在返回System.Object。我的问题是DataContractJsonSerializer在这个对象中使用和存储的类型是什么?