如何在c#中反序列化json表和值



我需要从json文件中获得一些信息,像这样:mcmodfile:

{
  "modListVersion": 2,
  "modList": [{
    "modid": "FloatingRuins",
    "name": "FloatingRuins",
    "description": "Floating islands taken from the ground with ruins on top.",
    "version": "1.7.10.r01",
    "mcversion": "1.7.10",
    "url": "http://www.minecraftforum.net/topic/1009577-/",
    "updateUrl": "",
    "authorList": [ "DaftPVF", "bspkrs" ],
    "credits": "Original by DaftPVF",
    "logoFile": "",
    "screenshots": [  ],
    "parent": "",
    "requiredMods": [ "bspkrsCore@[6.12,)" ],
    "dependencies": [ "bspkrsCore@[6.12,)" ],
    "dependants": [  ],
    "useDependencyInformation": "true"
  }]
}

我需要"ModListVersion","mcversion","version","name"one_answers"modid"。

我创建了一个类:

{
    class mcmod2
    {
        [JsonProperty("modListVersion")]
        public int modListVersion;
        [JsonProperty("modList")]
        public DataRow modList;
    }
}

我试图用以下代码获取数据:

String json;
using (StreamReader r = new StreamReader(mcmodfile)) 
{
    json = r.ReadToEnd();
}
mcmod2 modinfo2;
modinfo2 = JsonConvert.DeserializeObject<mcmod2>(json);

然而,当我试图反序列化文件时,我得到以下错误:http://paste.ubuntu.com/8071687/

注意我没有办法改变Json,它必须保持原样。

我尝试实现以下内容:我如何反序列化JSON数组并忽略根节点?没有帮助

这是从这里来的:http://json2csharp.com/#

public class ModList {
    public string modid { get; set; }
    public string name { get; set; }
    public string description { get; set; }
    public string version { get; set; }
    public string mcversion { get; set; }
    public string url { get; set; }
    public string updateUrl { get; set; }
    public List<string> authorList { get; set; }
    public string credits { get; set; }
    public string logoFile { get; set; }
    public List<object> screenshots { get; set; }
    public string parent { get; set; }
    public List<string> requiredMods { get; set; }
    public List<string> dependencies { get; set; }
    public List<object> dependants { get; set; }
    public string useDependencyInformation { get; set; } 
}
public class RootObject {
    public int modListVersion { get; set; }
    public List<ModList> modList { get; set; } 
}

然后把你已经有的代码替换成:

String json;
using (StreamReader r = new StreamReader(mcmodfile)) 
{
    json = r.ReadToEnd();
}
RootObject root = JsonConvert.DeserializeObject<RootObject>(json);

尝试创建一个新的c#类来捕获这些属性(看起来像一个具有字符串和数组属性的类):

可以命名为ModItem:

"modid": "FloatingRuins",
"name": "FloatingRuins",
"description": "Floating islands taken from the ground with ruins on top.",
"version": "1.7.10.r01",
"mcversion": "1.7.10",
"url": "http://www.minecraftforum.net/topic/1009577-/",
"updateUrl": "",
"authorList": [ "DaftPVF", "bspkrs" ],
"credits": "Original by DaftPVF",
"logoFile": "",
"screenshots": [  ],
"parent": "",
"requiredMods": [ "bspkrsCore@[6.12,)" ],
"dependencies": [ "bspkrsCore@[6.12,)" ],
"dependants": [  ],
"useDependencyInformation": "true"

然后将根类更改为:(确保它是public类)

public class mcmod2
{
    [JsonProperty("modListVersion")]
    public int modListVersion;
    [JsonProperty("modList")]
    public ModItem[] modList;
}

尝试使用这些对象:

public class ModList{
    [JsonProperty("modid")]
    public String ModeId { get; set; }
    [JsonProperty("name")]
    public String Name { get; set; }
    [JsonProperty("description")]
    public String Description { get; set; }
    [JsonProperty("version")]
    public String Version { get; set; }
    [JsonProperty("mcversion")]
    public String McVersion { get; set; }
    [JsonProperty("url")]
    public String Url { get; set; }
    [JsonProperty("updateUrl")]
    public String UpdateUrl { get; set; }
    [JsonProperty("authorList")]
    public List<String> AuthorList { get; set; }
    [JsonProperty("credits")]
    public String Credits { get; set; }
    [JsonProperty("logoFile")]
    public String LogFile { get; set; }
    [JsonProperty("screenshots")]
    public List<object> ScreenShots { get; set; }
    [JsonProperty("parent")]
    public String Parent { get; set; }
    [JsonProperty("requiredMods")]
    public List<String> RequiredMods { get; set; }
    [JsonProperty("dependencies")]
    public List<String> Dependencies { get; set; }
    [JsonProperty("dependants")]
    public List<object> Dependants { get; set; }
    [JsonProperty("useDependencyInformation")]
    public String UseDependencyInformation { get; set; }
}
public class McMod{
    [JsonProperty("modListVersion")]
    public Int32 ModListVersion { get; set; }
    [JsonProperty("modList")]
    public List<ModList> Mods { get; set; }
}

反序列化代码:

String json;
using (StreamReader r = new StreamReader(mcmodfile)) {
    json = r.ReadToEnd();
}
var deserializedObject = JsonConvert.DeserializeObject<McMod>(json);

相关内容

  • 没有找到相关文章

最新更新