1

I'm struggling to convert a json to an c# object.

My json is generated by an other app (in typescript) here is the structure of the object used to generate my json :

enter image description here

With this structure i'm able to create json like:

{
    EQNode: { key: "userid", value: 5}
}

Or more complexe :

{
    AndNode: [
        {
            EQNode: { key: "userid", value: 5 }
        },
        {
            LikeNode: { key: "username", value: "foo" }
        }
    ]
}

Or even more complexe :

{
    AndNode: [
        {
            EQNode: { key: "userid", value: 5 }
        },
        {
            OrNode: [
                {
                    LikeNode: { key: "username", value: "foo" }
                },
                {
                    LikeNode: { key: "email", value: "foo@bar.fr" }
                },
            ]
        }
    ]
}

I'have managed to parse my json using this model :

public class EQNode
{
    [JsonProperty("key")]
    public string key { get; set; }

    [JsonProperty("value")]
    public int value { get; set; }
}

public class LikeNode
{
    [JsonProperty("key")]
    public string key { get; set; }

    [JsonProperty("value")]
    public string value { get; set; }
}

public class GTNode
{
    [JsonProperty("key")]
    public string key { get; set; }

    [JsonProperty("value")]
    public int value { get; set; }
}

public class LTNode
{
    [JsonProperty("key")]
    public string key { get; set; }

    [JsonProperty("value")]
    public int value { get; set; }
}

public class OrNode
{
    [JsonProperty("EQNode")]
    public EQNode EQ { get; set; }
}

public class AndNode
{
    [JsonProperty("EQNode")]
    public EQNode EQ { get; set; }

    [JsonProperty("LikeNode")]
    public LikeNode LIKE { get; set; }

    [JsonProperty("GTNode")]
    public GTNode GT { get; set; }

    [JsonProperty("LTNode")]
    public LTNode LT { get; set; }

    [JsonProperty("OrNode")]
    public List<OrNode> OR { get; set; }

    [JsonProperty("AndNode")]
    public List<OrNode> And { get; set; }
}

public class RootObject
{
    [JsonProperty("AndNode")]
    public List<AndNode> And { get; set; }

    [JsonProperty("OrNode")]
    public List<OrNode> OR { get; set; }

    [JsonProperty("EQNode")]
    public EQNode EQ { get; set; }

    [JsonProperty("LikeNode")]
    public LikeNode LIKE { get; set; }

    [JsonProperty("GTNode")]
    public GTNode GT { get; set; }

    [JsonProperty("LTNode")]
    public LTNode LT { get; set; }
}

But this seems a little "hard coded" to me. Is there a way to have a nice / better model structure for my model ?

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Nicolas Pretot
  • 182
  • 2
  • 12
  • 1
    It's not clear to me why you've got four different node classes that all have the same structure. Couldn't that just be `UnaryNode`? (I'd also strongly recommend that you start following .NET naming conventions for your properties.) – Jon Skeet Nov 20 '17 at 11:04
  • check my answer and take help of visual studio ..no need to go in detail of understanding structure of json , visual studio do work for you – Pranay Rana Nov 20 '17 at 11:09
  • Just so you're aware, as long as the JSON names in the attribute brackets like you have, you can name the C# classes and properties whatever you want. – Sean O'Neil Nov 20 '17 at 11:10
  • In JSON names are enclosed into "", i.e. { "key": "123" }, not { key: "123" } – Igors Sutovs Nov 20 '17 at 11:10
  • Hi, thanks for your answer , in fact all exemple in my original post was for demonstration purpose and was not my actual code or jsons, i will update my post – Nicolas Pretot Nov 20 '17 at 13:31
  • To avoid confusion, i started from fresh here, sorry for double post : https://stackoverflow.com/questions/47394775/deserialize-json-using-c-sharp-with-variable-content?noredirect=1#comment81743134_47394775 – Nicolas Pretot Nov 20 '17 at 15:14
  • rolled back the revision which was moved to a different post – Bhargav Rao Dec 18 '17 at 12:30

2 Answers2

6

If you have Json with you than you can generate C# class using visual studio itself.

In visual studio Find "Paste Sepcial" menu. i.e. copy you json string and click on Paste special , it will generate C# class for you.

you can follow my post : Generate Class From JSON or XML in Visual Studio

Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
1

It would be really better if you decide on a specific JSON schema and use it in both your apps. A more detailed JSON would help me give you a better insight.

inthevortex
  • 334
  • 5
  • 20
  • Thanks for your answer i will update my post – Nicolas Pretot Nov 20 '17 at 13:28
  • To avoid confusion, i started from fresh here, sorry for double post : https://stackoverflow.com/questions/47394775/deserialize-json-using-c-sharp-with-variable-content?noredirect=1#comment81743134_47394775 – Nicolas Pretot Nov 20 '17 at 15:14