Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a list of JSON property names from a class to use in a query string

Tags:

If I have a C# model class that is used by JSON.net to bind data from a serialized JSON string, is there a way that I can create a query string from that class in order to make the initial request?

Model class example:

public class model {    [JsonProperty(PropertyName = "id")]    public long ID { get; set; }    [JsonProperty(PropertyName = "some_string")]    public string SomeString {get; set;}  } 

Querystring example:

baseUrl + uri + "&fields=id,some_string" + token 

So the essence of what I am trying to do is gather both "id" and "some_string" from the model object so i can dynamically create a the "&fields" arguments. Thanks!

like image 982
CostelloNicho Avatar asked Nov 09 '15 18:11

CostelloNicho


People also ask

What is property name in JSON?

Objects are the mapping type in JSON. They map “keys” to “values”. In JSON, the “keys” must always be strings. Each of these pairs is conventionally referred to as a “property”.

What is JSON property C#?

JsonPropertyAttribute indicates that a property should be serialized when member serialization is set to opt-in. It includes non-public properties in serialization and deserialization. It can be used to customize type name, reference, null, and default value handling for the property value.


2 Answers

@Leigh Shepperson has the right idea; however, you can do it with less code using LINQ. I would create a helper method like this:

using System.Linq; using System.Reflection; using Newtonsoft.Json; ...  public static string GetFields(Type modelType) {     return string.Join(",",         modelType.GetProperties()                  .Select(p => p.GetCustomAttribute<JsonPropertyAttribute>())                  .Select(jp => jp.PropertyName)); } 

You can use it like this:

var fields = "&fields=" + GetFields(typeof(model)); 

EDIT

If you're running under the 3.5 version of the .Net Framework such that you don't have the generic GetCustomAttribute<T> method available to you, you can do the same thing with the non-generic GetCustomAttributes() method instead, using it with SelectMany and Cast<T>:

    return string.Join(",",         modelType.GetProperties()                  .SelectMany(p => p.GetCustomAttributes(typeof(JsonPropertyAttribute))                                    .Cast<JsonPropertyAttribute>())                  .Select(jp => jp.PropertyName)                  .ToArray()); 
like image 66
Brian Rogers Avatar answered Sep 30 '22 02:09

Brian Rogers


You can do this using reflection. This is the general idea:

using Newtonsoft.Json; using Newtonsoft.Json.Serialization; using System.Reflection;  namespace ConsoleApplication8 {     public class model     {         [JsonProperty(PropertyName = "id")]         public long ID { get; set; }          [JsonProperty(PropertyName = "some_string")]         public string SomeString { get; set; }     }      internal class Program     {         private static void Main(string[] args)         {             var model = new model();              var result = string.Empty;              PropertyInfo[] props = typeof(model).GetProperties();             foreach (PropertyInfo prop in props)             {                 foreach (object attr in prop.GetCustomAttributes(true))                 {                     result += (attr as JsonPropertyAttribute).PropertyName;                 }             }         }     } } 
like image 34
Leigh Shepperson Avatar answered Sep 30 '22 02:09

Leigh Shepperson