将字典从.txt文件转换为C#变量
我需要帮助将字典从某些.txt
文件转换为c#中的变量。查看下面的这张照片(.txt
文件):
如您所见,有一个字典,其中其他两个字典。我想问一下是否可以将其转换为实际的C#字典。现在,.txt
文件显然与C#不符,因为我只是不知道该怎么做或是否可能。
我在下面有此代码只是打开文件并按行读取IT:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
public class MapLoader: MonoBehaviour
{
void Start()
{
string mapID = "SGT589H";
string path = $"Maps/{mapID}.txt";
Dictionary<string, string> map = new Dictionary<string, string>();
using (var file = new StreamReader(path))
{
string line = null;
bool skip = false;
while ((line = file.ReadLine()) != null)
{
........
}
}
}
}
不要介意Unity Engine,在我的问题中没关系。因此,我的目标是从.txt
文件中获取字典,然后将其保存在名为“映射”的C#字典中,以便我以后可以使用该字典(它将根据数据生成映射,我将把它们保存为.txt
文件,而不是统一场景,以保存空间,然后使用我的maphandler加载它们)。如您所见,&lt; string,String&gt;
也不对应。
我的问题是,有什么办法如何实现这一目标,或者您可能会给我一些关于我如何能够改变结构以实现这一目标的想法?谢谢。
I need help with converting a dictionary from some .txt
file to a variable in C#. Take a look at this picture below (the .txt
file):
As you can see, there is a dictionary in which are other two dictionaries. I want to ask if it is possible to convert it to an actual C# dictionary. Now, the .txt
file does not correspond to C# obviously, because I just don't know how to do that or if it is even possible.
I have this code below to just open the file and read it line by line:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
public class MapLoader: MonoBehaviour
{
void Start()
{
string mapID = "SGT589H";
string path = quot;Maps/{mapID}.txt";
Dictionary<string, string> map = new Dictionary<string, string>();
using (var file = new StreamReader(path))
{
string line = null;
bool skip = false;
while ((line = file.ReadLine()) != null)
{
........
}
}
}
}
Don't mind the Unity Engine, it doesn't matter in my question. So my goal is to take the dictionary from the .txt
file and save it in the C# dictionary named "map" so I can work with the dictionary later (it will be generating maps based on the data, I will be saving them as .txt
files, not as unity scenes, to save space and then use my MapHandler to load them). As you can see, the <string, string>
doesn't correspond too.
My question is, is there any way how to achieve that or could you possibly give me some ideas on how I would be able to change the structure to be able to do that? Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您决定自己解释这种类似JSON的格式的路径,则需要编写序列化器将Python结构转换为C#,因为您需要考虑不同类型的对象(数组,布尔值,整数和浮点),这可能会变得非常复杂。 ,您如何读取每条线和符号等。
另外,您正在尝试将不同类型的值(同样,阵列,布尔值,整数)存储到字符串中的事实将使您在尝试尝试时序列化这些值的序列化。访问并以后使用。
如果您可以更改输入的方式和哪种格式,我建议您坚持使用纯JSON,并使用任何第三方库或扩展名来解释它(第一个Google搜索给了我官方MS Docs和 json.net )。
当然,存储数据的方法会更改(即可能不仅仅是一个字典对象),但是它肯定会更有条理且易于使用。
If you decide to take path of interpreting this json-like format yourself you need to write serializer to convert Python structures into C# which can get quite complicated as you need to think about different types of objects (arrays, booleans, integers and floating points), how you read each line and symbol etc.
Plus, the fact that you're trying to store values of different types (again, arrays, booleans, integers) into String will give you a headache of serialization of those values when you try to access and use them later.
If you can change how and in which format you get the input, I suggest you to stick to pure JSON for this case and interpret it using any third-party library or extension (first Google search gave me official MS docs and Json.NET).
Of course, method of storing data will change (that is, probably more than a single Dictionary object) but it definitely will be more organized and easy to use.