0

I need to parse a jsonfile that has a structure like this:

"number": {
    "1": {
      "branch": null,
      "build": 1,
      "files": [
        [
          "zip",
          "client",
          "4d96d6e8f1543c5fa1184f4771ce16e2"
        ],
        [
          "zip",
          "src",
          "fd397591148fac49a7d57aafdccac6a3"
        ],
        [
          "zip",
          "server",
          "d7e1df9a91ded33be81ee8226b027c2f"
        ],
        [
          "txt",
          "changelog",
          "df98aec1a868ce99532c64f246387d55"
        ]
      ],
      "jobver": "1.3.2",
      "mcversion": "1.1",
      "modified": 1328269373.0,
      "version": "1.3.2.01"
    },
    "2": {
      "branch": null,
      "build": 2,
      "files": [
        [
          "zip",
          "server",
          "80fbd5d837a5867c2dd7b7967e3aa2a9"
        ],
        [
          "zip",
          "client",
          "0cedb5e9844e490f877b6cf04601f929"
        ],
        [
          "txt",
          "changelog",
          "87e9fba9322e9dbc2ea482a2c3edeec6"
        ],
        [
          "zip",
          "src",
          "292d5596879bd13c159a2afe571ec5eb"
        ]
      ],
      "jobver": "1.3.2",
      "mcversion": "1.1",
      "modified": 1328613907.0,
      "version": "1.3.2.2"
    }

(The entire json file can be found here: http://files.minecraftforge.net/maven/net/minecraftforge/forge/json)
I have figure out the internals, in the "1" and the externals that leads to this code part, however using the Json.NET library I can't figure out how to deal with the "1" and "2"s without writing a seperate class for each. I have over 1000 of these to parse trough, so I need some way that can do it relatively simple. My current class for parsing looks something like this:

public class forgemaven
{
    public string homepage { get; set; }
    public string name { get; set; }
    public List<Number> number { get; set; }
    public string webpath { get; set; }
}

public class Number
{
    public string branch { get; set; }
    public int build { get; set; }
    public List<String> files { get; set; }
    public string jobver { get; set; }
    public string mcversion { get; set; }
    public string modified { get; set; }
    public string version { get; set; }
}

Note: I removed some of the things, like the first 1000 lines of version/build, since they aren't needed and json.net seems fine with that.
I have tried parsing it with http://json2csharp.com/ sadly that crashes my webbrowser. I also tried the desktop version of it, but that give me almost a MB of classes, which again isn't optimal, since new builds are constantly added to this file, so I would have to change my structure all the time, and it would be horrible to use the data from here.
So for my question: How do I do this in a maintainable way?

1

2 Answers 2

1

Your problem is that {"1" : something, "2" ; something} is not a json array, it's a json map.

In .net, you will want to deserialize that to a Dictionary<int,TObject>. Then you can process it afterwards if it really should be a `List' or an array, but you can't do that while deserializing: you got bad json in the first place, you have to deal with it.

Here is a fiddle demonstrating the thing (with the json you gave and your own classes only very slightly modified: the files property is a List<List<string>> instead of a List<String>) using Json.Net : https://dotnetfiddle.net/feqFZd

Sign up to request clarification or add additional context in comments.

Comments

0

You must replace

public List<Number> number { get; set; }

with

public Dictionary<int,Number> number { get; set; }

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.