简体   繁体   中英

How to convert dynamic JSON to C# object

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.

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

在此处输入图像描述

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM