Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MonoGame-通过内容管道加载JSON_C#_Json_Monogame - Fatal编程技术网

C# MonoGame-通过内容管道加载JSON

C# MonoGame-通过内容管道加载JSON,c#,json,monogame,C#,Json,Monogame,我正在创建一个RPG游戏,我需要加载带有平铺的地图。我将平铺部分放下(我使用的是MonoGame.Extended)。但是我需要关于地图的额外数据。我的计划是使用包含必要信息的JSON文件。但是,我希望通过内容管道获得它,因为它与平铺贴图直接相关 我尝试使用自定义内容管道扩展。它使用JSON.Net将JSON文件反序列化为字典。然而,当我编译DLL文件并试图将其引用到管道工具中时,管道工具总是会崩溃 内容导入器: using System; using System.Collections.Ge

我正在创建一个RPG游戏,我需要加载带有平铺的地图。我将平铺部分放下(我使用的是
MonoGame.Extended
)。但是我需要关于地图的额外数据。我的计划是使用包含必要信息的JSON文件。但是,我希望通过内容管道获得它,因为它与平铺贴图直接相关

我尝试使用自定义内容管道扩展。它使用
JSON.Net
将JSON文件反序列化为
字典
。然而,当我编译DLL文件并试图将其引用到管道工具中时,管道工具总是会崩溃

内容导入器:

using System;
using System.Collections.Generic;
using System.IO;

using Microsoft.Xna.Framework.Content.Pipeline;

using Newtonsoft.Json;

namespace JsonExtension
{

    [ContentImporter(".json", DefaultProcessor = "JsonProcessor")]
    public class JsonImporter : ContentImporter<Dictionary<string, dynamic>>
    {

        public override Dictionary<string, dynamic> Import(string filename, ContentImporterContext context)
        {
            context.Logger.LogMessage("Importing JSON file: {0}", filename);

            using (var streamReader = new StreamReader(filename))
            {
                JsonSerializer serializer = new JsonSerializer();
                return (Dictionary<string, dynamic>)serializer.Deserialize(streamReader, typeof(Dictionary<string, dynamic>));
            }
        }

    }

}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用Microsoft.Xna.Framework.Content.Pipeline;
使用Newtonsoft.Json;
命名空间JsonExtension
{
[ContentImporter(“.json”,DefaultProcessor=“JsonProcessor”)]
公共类JsonImporter:ContentImporter
{
公共覆盖字典导入(字符串文件名,ContentImporterContext)
{
LogMessage(“导入JSON文件:{0}”,文件名);
使用(var streamReader=newstreamreader(文件名))
{
JsonSerializer serializer=新的JsonSerializer();
return(Dictionary)序列化程序。反序列化(streamReader,typeof(Dictionary));
}
}
}
}
内容处理器:

using System;
using System.Collections.Generic;

using Microsoft.Xna.Framework.Content.Pipeline;

namespace JsonExtension
{
    [ContentProcessor]
    public class JsonProcessor : ContentProcessor<Dictionary<string, dynamic>, Dictionary<string, dynamic>>
    {
        public override Dictionary<string, dynamic> Process(Dictionary<string, dynamic> input, ContentProcessorContext context)
        {
            context.Logger.LogMessage("Processing JSON");

            return input;
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用Microsoft.Xna.Framework.Content.Pipeline;
命名空间JsonExtension
{
[内容处理器]
公共类JsonProcessor:ContentProcessor
{
公共重写字典进程(字典输入、ContentProcessor上下文)
{
LogMessage(“处理JSON”);
返回输入;
}
}
}
内容类型编写器:

using System;
using System.Collections.Generic;
using System.IO;

using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Compiler;

using Newtonsoft.Json;

namespace JsonExtension
{
    [ContentTypeWriter]
    class JsonTypeWriter : ContentTypeWriter<Dictionary<string, dynamic>>
    {
        protected override void Write(ContentWriter output, Dictionary<string, dynamic> value)
        {
            output.Write(JsonConvert.SerializeObject(value));
        }

        public override string GetRuntimeType(TargetPlatform targetPlatform)
        {
            return typeof(Dictionary<string, dynamic>).AssemblyQualifiedName;
        }

        public override string GetRuntimeReader(TargetPlatform targetPlatform)
        {
            return "JsonExtension.JsonReader";
        }
    }
}

使用系统;
使用System.Collections.Generic;
使用System.IO;
使用Microsoft.Xna.Framework.Content.Pipeline;
使用Microsoft.Xna.Framework.Content.Pipeline.Serialization.Compiler;
使用Newtonsoft.Json;
命名空间JsonExtension
{
[内容打字机]
类JsonTypeWriter:ContentTypeWriter
{
受保护的重写无效写入(ContentWriter输出,字典值)
{
Write(JsonConvert.SerializeObject(值));
}
公共重写字符串GetRuntimeType(TargetPlatform TargetPlatform)
{
返回类型(字典).AssemblyQualifiedName;
}
公共重写字符串GetRuntimeReader(TargetPlatform TargetPlatform)
{
返回“JsonExtension.JsonReader”;
}
}
}

有没有更好的方法来存储平铺地图元数据?还是有更好的方法通过管道导入?或者我的内容导入器有问题吗?

错误是由于
动态类型造成的。管道要求所有类型都可以通过反射解析和实例化

泛型集合有一个辅助解析器,用于序列化和反序列化泛型集合中的每个类型。行
Dictionary
有两种类型和与
System.Collections.Generic.Dictionary
System.String
dynamic
关联的不可知类型。
动态
没有关于使用哪种类型的提示,因此无法解析,从而导致错误

简单的解决方案是将数据存储为json字符串(您现在已经在这样做了,但您试图存储它的类型是
dynamic
),并在
Content.Load
返回数据后将
字符串反序列化到其对象中

换句话说,使用
字典
作为内容处理器类型



更简单的解决方案是将
JSON
文件复制到输出目录。在Game1的
Initialize
方法中,构建操作“copy if newer”,并使用System.IO.File读取该操作,并使用
JSON.NET
将其反序列化为对象。

与崩溃相关的错误消息是什么?您可以将平铺中的自定义属性添加到地图、图层甚至平铺中的每个对象。我使用TiledSharp而不是Monogame.Extended,但我确信它也支持使用自定义道具。
System.Reflection.ReflectionTypeLoadException:无法加载一个或多个请求的类型。检索LoaderExceptions属性以获取更多信息,该信息是来自内容管道的错误消息