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

    }
7
  • Your JSON doesn't contain a list, it's normal that the converter didn't pick that up. Commented Jun 29, 2018 at 19:57
  • It is not really.. this question has 2 extra problems first "type_1" elements are dynamic and not in list Commented Jun 29, 2018 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? Commented Jun 29, 2018 at 19:58
  • @Svek not really .. internally these are file names. which could be in any variety. Commented Jun 29, 2018 at 20:00
  • ummm.... Do you know the filenames? More specifically, are they available in an array somewhere? Commented Jun 29, 2018 at 20:01

1 Answer 1

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;
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

I thought of that too.. but what's gonna happen with other properties? which are there in too
You can break it up into an additional step. Go through the dictionary and pull out the keys that you already know, and convert accordingly. --- I'll try and add some sample code for you. Gimme a sec to update the answer.
@abhishek -- how about something like this?
OK. let me try this,
@abhishek did this solve your problem?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.