相关疑难解决方法(0)

asp.net core 1.0 web api使用camelcase

在RC2相同的代码返回json格式与camel case.在netcore 1.0发布之后,我启动了新项目,同样的代码以小写形式返回json.

尝试了多个解决方案,但没有一个正在使用 web-api-serialize-properties-starting-from-lowercase-letter

c# json json.net asp.net-core-mvc asp.net-core-1.0

48
推荐指数
2
解决办法
2万
查看次数

获取属性的 JsonPropertyAttribute

我找到了一个帖子,对我遇到的问题有很好的答案,但我似乎找不到我正在寻找的小细节。

public class myModel
{
   [JsonProperty(PropertyName = "id")]
   public long ID { get; set; }
   [JsonProperty(PropertyName = "some_string")]
   public string SomeString {get; set;} 
}
Run Code Online (Sandbox Code Playgroud)

我需要一个返回JsonProperty PropertyName特定属性的方法。也许我可以传递Type和Property我需要的东西,如果找到,该方法将返回该值。

这是我找到的方法,它使我朝着正确的方向(我相信)从这里获取

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>()
                 .Where(jp => jp != null)
                 .Select(jp => jp.PropertyName));
}
Run Code Online (Sandbox Code Playgroud)

目标是调用这样的函数(任何修改都可以)

string field = GetField(myModel, myModel.ID);
Run Code Online (Sandbox Code Playgroud)

更新 #1

我修改了上面的这个,但我不知道如何获取IDfrom的字符串myModel.ID。

public static string GetFields(Type modelType, …
Run Code Online (Sandbox Code Playgroud)

c# reflection lambda properties json.net

4
推荐指数
1
解决办法
4836
查看次数

如何在 C# 中使用反射获取 Json 属性名称

我有一堂课,如下所述:

public class Employee {

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

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

    [JsonProperty("emp_lname")]
    public string LastName {get;set;} 
}
Run Code Online (Sandbox Code Playgroud)

在上面的类中,我为序列化分配了 Newtonsoft 属性。

我有一个类对象Employee,现在我想找到该属性JsonProperty并获取该属性的值。

例如,我想获取 JsonProprty 属性名称设置为的属性的值emp_lname

有没有办法使用反射来找到这样的价值?

c# json.net system.reflection

4
推荐指数
1
解决办法
1万
查看次数