0

I am trying to convert below JSON object to C# class. I could able to get C# equivalent for filter, but not for sort.

In the case of filter JSON object; andOr, openCondition, etc are static. Hence, I could able to generate C# class.

But for sort JSON object; accountName, and tradeDate are not static. These fields are completely as per user requirement. They may change as some other fields for some other input. Hence, I'm not understanding how can I generate C# class for this sort JSON object. Can anyone please suggest me how to do this?

{
    "filter": [
        {
            "andOr": "",
            "openCondition": false,
            "columnName": "accountName",
            "operator": "eq",
            "value": "KATHERINE",
            "closeCondition": false
        }
    ],
    "sort": {
            "accountName": "asc",
            "tradeDate": "desc"       
    },
    "pageIndex": 1,
    "pageSize": 75
}

I have tried to create SortCriteria class like below. But, it is not matching to the JSON sort object.

public class SortCriteria
{
    public string ColumnName { get; set; }
    public string SortOrder { get; set; }
}

Can anyone please suggest me how can I solve this issue.

1
  • That is why you have newtonsoft package Commented Oct 5, 2019 at 9:58

3 Answers 3

1

You could use a Dictionary (with JsonExtensionDataAttribute) for cases where properties are NOT fixed, such as in Sort. For example,

public class Filter
{
    public string andOr { get; set; }
    public bool openCondition { get; set; }
    public string columnName { get; set; }
    public string @operator { get; set; }
    public string value { get; set; }
    public bool closeCondition { get; set; }
}

public class Sort
{
    [JsonExtensionData]
    public Dictionary<string,object> RandomKeyValuePair {get;set;}
}

public class RootObject
{
    public List<Filter> filter { get; set; }
    public Sort sort { get; set; }
    public int pageIndex { get; set; }
    public int pageSize { get; set; }
}

Sample Output

enter image description here

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

Comments

1

If your json properties are not fixed and it may change over the time then instead of creating a separate class you can use dynamic.

Like,

string jsonString = @"{
    sort: {
      accountName: 'asc',
      tradeDate: 'desc'       
    }
}";


JObject obj = JObject.Parse(jsonString);
Console.WriteLine($"AccountNumber: {obj["sort"]["accountName"]}");

using Json path,

Console.WriteLine($"AccountNumber: {obj.SelectToken("$.sort.accountName")}");

.Net Fiddle

Comments

1

You can deserialize any JSON object into a Dictionary<string,object> or if you know the value type you can use that value type instead of object.

In this case the sort value will only be "asc" or "desc" so we can use a string or (better) an enum

public enum SortDirection
{
    Asc,
    Desc,
}

The class would look like

public class Data
{
    ...
    [JsonProperty( "sort" )]
    public Dictionary<string,SortDirection> Sort { get; set; }
    ...
}

a minimal example

var json = "{\"sort\":{\"accountName\":\"asc\",\"tradeDate\":\"desc\"}}";
var obj = JsonConvert.DeserializeObject<Data>(json);

.net fiddle example

2 Comments

Thank you for your answer. "Anu Viswan" answer worked well for me, hence, marked as accepted answer. Your suggestion is also valuable. Hence, given up-vote. I have not yet verified your suggestion, but looks like nice approach.
The verification is very simple: click on the .net fiddle link in the answer and see the working code sample in your browser ;o)

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.