我需要从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”和“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,它必须保持原样。
我尝试实施以下内容:How do I deserialize a JSON array and ignore the root node? 没帮忙
答案 0 :(得分:1)
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);
答案 1 :(得分:0)
尝试创建一个新的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;
}
答案 2 :(得分:0)
尝试使用这些对象:
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);