将 JSON 作为资源加载到对象

我有一个大的 JSON 文件,我想将其加载到我的项目中。然后我想搜索名称并返回类型。示例如下:


[

  {

    "Name": "Name1",

    "Type": "TypeA"

  },

  {

    "Name": "Name2",

    "Type": "TypeB"

  }

]

我的问题是我不确定最好的方法是什么。我应该创建一个对象类然后将文件加载到其中吗?


我一直在尝试通过从我的资源中加载它来做到这一点。我似乎做不到,或者找不到任何其他人这样做的例子,这让我相信我做错了。我试过如下:


var resourceName = FS.Properties.Resources.types;

dynamic jsonDe = JsonConvert.DeserializeObject<TypeList>(resourceName);


dynamic jsonDe = JsonConvert.DeserializeObject<TypeList>("FS.Resources.types.json");

任何建议都将非常受欢迎。谢谢


慕沐林林
浏览 122回答 4
4回答

江户川乱折腾

一种方法是反序列化 Json 文件并将其存储在数据类中。当 Json 文件转换为包含信息的数据类对象列表时,可以使用 Linq 搜索属性的给定值。请参阅以下链接以反序列化 JSON 文件Deserialize Json From File。Linq 查询如下所示。arrayList.where(w&nbsp;=>&nbsp;w.propertyName&nbsp;==&nbsp;"value&nbsp;of&nbsp;string").select(s&nbsp;=>&nbsp;s.propertyName)..ToList()如果您需要单个属性,则可以使用 或 检索匹配属性的列表.First()。请注意,.First()当找不到匹配值时会抛出异常。可以使用.FirstOrDefault(),这会返回一个空值,因为没有匹配值。使用该.FirstOrDefault()方法时,您可以添加一个空检查,如果找到了您要查找的值,就会显示一条消息。

RISEBY

从这里:如何读取嵌入式资源文本文件您可以使用Assembly.GetManifestResourceStream方法:使用 System.IO添加以下用法;使用 System.Reflection;设置相关文件的属性:Build Action带值的&nbsp;参数Embedded Resource使用以下代码var assembly = Assembly.GetExecutingAssembly();var resourceName = "MyCompany.MyProduct.MyFile.txt";using (Stream stream = assembly.GetManifestResourceStream(resourceName))using (StreamReader reader = new StreamReader(stream)){&nbsp; &nbsp; string result = reader.ReadToEnd();}resourceName是嵌入在 中的资源之一的名称assembly。例如,如果您嵌入一个名为的文本文件"MyFile.txt",该文件位于具有默认命名空间的项目的根目录中"MyCompany.MyProduct",则 resourceName 是"MyCompany.MyProduct.MyFile.txt".您可以使用 Assembly.GetManifestResourceNames 方法获取程序集中所有资源的列表。仅从文件名中获取 resourceName 的明智之举(通过命名空间的东西):string resourceName = assembly.GetManifestResourceNames()&nbsp; .Single(str => str.EndsWith("YourFileName.txt"));之后,您可以使用这个或这个 (之前由用户建议)来反序列化您的值。

呼如林

我认为这是最干净、最容易理解的解决方案:public class Program{&nbsp; public class MappedObject&nbsp; {&nbsp; &nbsp; public string Name { get; set; }&nbsp; &nbsp; public string Type { get; set; }&nbsp; }&nbsp; public static void Main(string[] args)&nbsp; {&nbsp; &nbsp; // search query&nbsp; &nbsp; string searchFor = "Name1";&nbsp; &nbsp; // our json&nbsp; &nbsp; string jsonData = "[{\"Name\": \"Name1\",\"Type\": \"TypeA\"},{\"Name\": \"Name2\",\"Type\": \"TypeB\"}]";&nbsp; &nbsp; // I'm mapping the json string into a list of MappedObject (s)&nbsp; &nbsp; List<MappedObject> mappedObjects = JsonConvert.DeserializeObject<List<MappedObject>>(jsonData);&nbsp; &nbsp; // I'm looping through this list to find the MappedObject&nbsp; &nbsp; // that matches the searchFor search query string&nbsp; &nbsp; foreach (MappedObject obj in mappedObjects)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; if (obj.Name == searchFor)&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // when I find it, I'll print the Type property&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(obj.Type);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; Console.ReadLine();&nbsp; }}如果你想从外部资源读取 json,比如.json你可以这样做的文件:public class Program{&nbsp; public class MappedObject&nbsp; {&nbsp; &nbsp; public string Name { get; set; }&nbsp; &nbsp; public string Type { get; set; }&nbsp; }&nbsp; public static void Main(string[] args)&nbsp; {&nbsp; &nbsp; // search query&nbsp; &nbsp; string searchFor = "Name1";&nbsp; &nbsp; using (StreamReader r = new StreamReader("file.json"))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; // our json read from file.json&nbsp; &nbsp; &nbsp; string jsonData = r.ReadToEnd();&nbsp; &nbsp; &nbsp; // I'm mapping the json string into a list of MappedObject (s)&nbsp; &nbsp; &nbsp; List<MappedObject> mappedObjects = JsonConvert.DeserializeObject<List<MappedObject>>(jsonData);&nbsp; &nbsp; &nbsp; // I'm looping through this list to find the MappedObject&nbsp; &nbsp; &nbsp; // that matches the searchFor search query string&nbsp; &nbsp; &nbsp; foreach (MappedObject obj in mappedObjects)&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (obj.Name == searchFor)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // when I find it, I'll print the Type property&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(obj.Type);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; Console.ReadLine();&nbsp; }}

慕标5832272

声明一个类来模仿您的 JSON 结构是一种选择:sealed class MyType{&nbsp; &nbsp; public string Name { get; set; }&nbsp; &nbsp; public string Type { get; set; }}然后您将能够在类型列表中反序列化它并根据给定名称进行过滤private static string FindType(string json, string name){&nbsp; &nbsp; return JsonConvert.DeserializeObject<List<MyType>>(json)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .SingleOrDefault(nt => nt.Name == name)?&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Type ?? "No type found";}请注意,此方法会评估是否找不到具有给定名称的记录,在这种情况下,它会返回默认字符串。如果要将此方法与资源文件一起使用:static void Main(string[] args){&nbsp; &nbsp; Console.WriteLine(FindType(FS.Properties.Resources.types, "Name1")); //prints "Type1&nbsp; &nbsp; Console.WriteLine(FindType(FS.Properties.Resources.types, "name1")); //prints "No type found&nbsp; &nbsp; Console.ReadKey();}
打开App,查看更多内容
随时随地看视频慕课网APP