-6

So here's my issue. I included a working example of a code that I'm trying to Parse into working C# Objects, but for the life of me I can't figure out how that would be possible with this type of JSON format. Can't even imagine how you access the first part of the nesting which uses quotes ( ""{ )as a name. So I figured I better as the experts here.

    {
    "": {
        "1": {
            "1": {
                "id": "1",
                "active": "1",
                "guidisplay_name": "NM",
                "image": null,
                "row": "1",
                "col": "1",
                "rack": "A"
            },
            "3": {
                "id": "3",
                "active": "1",
                "name": "NM",
                "image": null,
                "row": "1",
                "col": "3",
                "rack": "A"
            },
            "4": {
                "id": "4",
                "active": "1",
                "guidisplay_name": "NM",
                "image": null,
                "row": "1",
                "col": "4",
                "rack": "A"
            },
            "5": {
                "id": "5",
                "active": "1",
                "guidisplay_name": "NM",
                "image": null,
                "row": "1",
                "col": "5",
                "rack": "A"
            },
            "6": {
                "id": "6",
                "active": "1",
                "name": "NM",
                "image": null,
                "row": "1",
                "col": "6",
                "rack": "A"
            },
            "2": {
                "id": "8",
                "active": "1",
                "name": "NM",
                "image": null,
                "row": "1",
                "col": "2",
                "rack": "A"
            }
        },
        "2": {
            "2": {
                "id": "2",
                "active": "1",
                "name": "NM",
                "image": null,
                "row": "2",
                "col": "2",
                "rack": "A"
            },
            "1": {
                "id": "7",
                "active": "1",
                "name": "NM",
                "image": null,
                "row": "2",
                "col": "1",
                "rack": "A"
            },
            "3": {
                "id": "9",
                "active": "1",
                "name": "NM",
                "image": null,
                "row": "2",
                "col": "3",
                "rack": "A"
            },
            "4": {
                "id": "10",
                "active": "1",
                "name": "NM",
                "image": null,
                "row": "2",
                "col": "4",
                "rack": "A"
            },
            "5": {
                "id": "11",
                "active": "1",
                "name": "NM",
                "image": null,
                "row": "2",
                "col": "5",
                "rack": "A"
            },
            "6": {
                "id": "12",
                "active": "1",
                "name": "NM",
                "image": null,
                "row": "2",
                "col": "6",
                "rack": "A"
            },
            "7": {
                "id": "13",
                "active": "1",
                "name": "NM",
                "image": null,
                "row": "2",
                "col": "7",
                "rack": "A"
            }
        }
    }
}
Brandon Minnick
  • 13,342
  • 15
  • 65
  • 123
Hec031
  • 1
  • 1
  • 1
    While there most likely is someone here that knows the answer, they are not here to do it for you. What have you tried? What are your thought process? How does the backing field even look? – Emz Oct 18 '17 at 18:57
  • Do you have any code to get values from the JSON at all? – 15ee8f99-57ff-4f92-890c-b56153 Oct 18 '17 at 18:57
  • You might find this similar question helpful. https://stackoverflow.com/questions/22191167/convert-json-string-to-c-sharp-object-list – J. Schmale Oct 18 '17 at 19:04
  • Sorry, but all I'm trying to find out is how to get around the " " quotes as a name, because C# will mark that as an error. I can Parse single objects from JSON to C# or more specifically Unity using the JSON Utility library, but this is a whole different animal. Again, not asking you guys to do my homework for me, just trying to see if you guys know how a case like this should be handled. I have looked for similar examples and I can't find any. Suspect this kind of formatting is super rare in JSON or way, way advanced. Thanks. – Hec031 Oct 18 '17 at 19:05
  • J. Schmale, You are the man. Thanks. That other question had a link to a quicktype parser, which assigned a value of A to the open quotes that I did not know how to handle. That's all I needed to at least move forward in solving this problem. More importantly I now have a tool that will allow me to solve issues like these with abstract JSON string object formats. Thanks. – Hec031 Oct 18 '17 at 19:26
  • Hi Guys thanks for all the help you guys provided on get around this mysterious empty quote " " issue. At the end it turns out that the empty quotes were in fact an error in the original JSON string. By the time this had become a fact I had already learned thanks to you guys that I could simple ignore them and use the "1" as my first viable string field. I'm just learning JSON. My experience is mostly in C# and Unity3d. The whole serializable field stuff has been a challenge to me but it's a matter of time and experience before this is all a bad memory. LOL. Again that's for all the help. – Hec031 Oct 20 '17 at 15:09

1 Answers1

3

All you need is declare a class like below

public class MyClass
{
    public string id { get; set; }
    public string active { get; set; }
    public string guidisplay_name { get; set; }
    public object image { get; set; }
    public string row { get; set; }
    public string col { get; set; }
    public string rack { get; set; }
}

and deserialize as

var obj = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, Dictionary<string,MyClass>>>>(json);
Eser
  • 12,346
  • 1
  • 22
  • 32