0

I need to get some info from json files like this: 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"
  }]
}

I need the "ModListVersion", "mcversion", "version", "name" and "modid".

I made a class:

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

        [JsonProperty("modList")]
        public DataRow modList;
    }
}

I tried to get the data with the following code:

String json;
using (StreamReader r = new StreamReader(mcmodfile)) 
{
    json = r.ReadToEnd();
}

mcmod2 modinfo2;
modinfo2 = JsonConvert.DeserializeObject<mcmod2>(json);

However i get the following error when I try to deserialize the file: http://paste.ubuntu.com/8071687/

Note I have no way of changing the Json, it will have to stay as is.

I tried implementing the following: How do I deserialize a JSON array and ignore the root node? Didn't help

3 Answers 3

1

This comes from here: 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; } 
}

And substitute into the code that you already have:

String json;
using (StreamReader r = new StreamReader(mcmodfile)) 
{
    json = r.ReadToEnd();
}

RootObject root = JsonConvert.DeserializeObject<RootObject>(json);
Sign up to request clarification or add additional context in comments.

2 Comments

You sir, are a saint. I would give you an upvote, but I don't have the rep yet.
@RasmusHansen You can also use File.ReadAllText(path) to read the whole file in one shot without need for a StreamReader.
0

Try creating a new C# class to capture these properties (looks like a class that has string and array properties):

Maybe name it 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"

Then change your root class to this: (make sure it's a public class)

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

    [JsonProperty("modList")]
    public ModItem[] modList;
}

Comments

0

Try using these objects:

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; }
}

Deserialization code:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.