0

I have the following JSON (not the true data) in the following format, which I am trying to convert into C# equivalent classes. If you noticed that type_1, type_2 these could be in any numbers. which is difficult to put in collection(sort of anonymous). If i remove all type_1, type_2 ... from the JSON it is easy can be converted to classes. Since it in dynamic in nature it is making it difficult.

I have tried http://json2csharp.com/ to get the classes. Problem is, it converts "types" as members, which is not true, it should be a dynamic collection or an array.

This slight different problem

This question has 2 extra problems first "type_1" elements are dynamic and second is these are not in list or an array.

I don't have control over this JSON format which is extra frustration.

{
    "type_1": {
        "@type": "blob",
        "content_type": "image/jpeg",
        "digest": "sha1-rCY0Tk8qRqaLfiQXdxYeX9Y+WMI="
    },
    "type_2": {
        "@type": "blob",
        "content_type": "image/jpeg",
        "digest": "sha1-rCY0Tk8qRqaLfiQXdxYeX9Y+WMI="
    },
    "type_3": {
        "@type": "blob",
        "content_type": "image/jpeg",
        "digest": "sha1-rCY0Tk8qRqaLfiQXdxYeX9Y+WMI=",
    },
    "type_4": {
        "@type": "blob",
        "content_type": "image/jpeg",
        "digest": "sha1-rCY0Tk8qRqaLfiQXdxYeX9Y+WMI=",
    },
    "type_5": {
        "@type": "blob",
        "content_type": "image/jpeg",
        "digest": "sha1-rCY0Tk8qRqaLfiQXdxYeX9Y+WMI=",
    },

    "References": {
        "blob_1": {
            "content_type": "image/jpeg",
            "digest": "sha1-rCY0Tk8qRqaLfiQXdxYeX9Y+WMI=",
            "revpos": 1,
            "stub": true
        },

        "blob_2": {
            "content_type": "image/jpeg",
            "digest": "sha1-rCY0Tk8qRqaLfiQXdxYeX9Y+WMI=",
            "revpos": 1,
            "stub": true
        }
    },
    "someproperty1": "somevalue1",
    "someproperty2": "somevalue2",
    "someproperty3": "somevalue3",
    "someproperty4": "somevalue4",
    "someproperty5": "somevalue5"
}

I was thinking something like

Filecollection is nothing but that anonymous collection; no luck ;(

public partial class Doc
    {
        [JsonProperty("references")]
        public Dictionary<String, Attachments> Attachments { get; set; }

        public List<Dictionary<String, Details>> FileCollections { get; set; }

        [JsonProperty("someproperty")]
        public String someproperty { get; set; }

        [JsonProperty("someproperty2")]
        public String someproperty2 { get; set; }

        [JsonProperty("someproperty3")]
        public String someproperty3 { get; set; }

    }
abhishek
  • 13
  • 6
  • Your JSON doesn't contain a list, it's normal that the converter didn't pick that up. – Haytam Jun 29 '18 at 19:57
  • It is not really.. this question has 2 extra problems first "type_1" elements are dynamic and not in list – abhishek Jun 29 '18 at 19:58
  • Is there a common pattern for the `type_1` and `type_2`? As in, is the name always the same (type) and followed by an underscore (_) and then the numeric increment? – Svek Jun 29 '18 at 19:58
  • @Svek not really .. internally these are file names. which could be in any variety. – abhishek Jun 29 '18 at 20:00
  • ummm.... Do you know the filenames? More specifically, are they available in an array somewhere? – Svek Jun 29 '18 at 20:01
  • @Svek -- these are coming from variety of sources and I don't have any knowledge or control over it. – abhishek Jun 29 '18 at 20:04
  • @abhishek -- I added an answer, well at least I think it is, based on your question and comments. – Svek Jun 29 '18 at 20:07

1 Answers1

0

Based on your comments (hopefully, I understand it)...

{
    "unknown_a" : {...},
    "unknown_b" : {...},
    "unknown_c" : {...},
    "unknown_d" : {...},
    "foo" : true,      // known property
    "bar" : {          // known property
        "x" : "xxx",   
        "y" : "yyy"
    }
}

I'm thinking that you want a Dictionary<string, object>?

Then you can do a little bit of your own conversion magic --- for example (just to get the concept out there)

foreach (d in dictionary)
{
    switch (d.Key)
    {
        case "foo": ... // known property
            obj.Foo = (bool)d.Value;
            break;
        case "bar": ... // known property
            obj.Bar = (Bar)d.Value;
            break;
        default: ...    // according to your comments, these are known types
            try
            {
                obj.Files.Add((File)d.Value);
            }
            catch {...}
            break;
    }
}
Svek
  • 12,350
  • 6
  • 38
  • 69