我做错了什么与JSON.NET's JsonConvert

本文关键字:JsonConvert NET JSON 错了 什么 | 更新日期: 2023-09-27 18:16:16

json string

{
  "success": true,
  "challenge_ts": "2016-11-03T17:30:00Z",
  "hostname": "mydomain.com"
}

internal class reCaptchaResponse
{
    internal bool success { get; set; }
    internal DateTime challenge_ts { get; set; }  // timestamp of the challenge load (ISO format yyyy-MM-dd'T'HH:mm:ssZZ)
    internal string hostname { get; set; }        // the hostname of the site where the reCAPTCHA was solved
    internal string[] error_codes { get; set; }   // optional
}

试图序列化

reCaptchaResponse responseObject = Newtonsoft.Json.JsonConvert.DeserializeObject<reCaptchaResponse>(jsonResult);

尝试失败,如…

Newtonsoft.Json.JsonConvert.SerializeObject(responseObject)返回{}

我做错了什么与JSON.NET's JsonConvert

Json。默认情况下,Net只序列化/反序列化public字段和属性,但您也可以在不将访问修饰符从internal更改为public的情况下执行此操作。

使用JsonProperty属性

internal class reCaptchaResponse
{
    [JsonProperty]
    internal bool success { get; set; }
    [JsonProperty]
    internal DateTime challenge_ts { get; set; }  // timestamp of the challenge load (ISO format yyyy-MM-dd'T'HH:mm:ssZZ)
    [JsonProperty]
    internal string hostname { get; set; }        // the hostname of the site where the reCAPTCHA was solved
    [JsonProperty]
    internal string[] error_codes { get; set; }   // optional
}

(不修改原始类)你甚至可以使用ContractResolver来选择在序列化过程中应该使用哪些属性/字段

编辑

虽然这个答案已经被接受了,但我想发布一个原始程序集不能修改的代码。

var settings = new JsonSerializerSettings() { 
                   ContractResolver = new AllPropertiesContractResolver() 
               };
reCaptchaResponse responseObject = 
                JsonConvert.DeserializeObject<reCaptchaResponse>(jsonResult ,settings);

public class AllPropertiesContractResolver : DefaultContractResolver
{
    protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
    {
        var props = type.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance)
                    .Select(x => new Newtonsoft.Json.Serialization.JsonProperty()
                    {
                        PropertyName = x.Name,
                        PropertyType = x.PropertyType,
                        Readable = true,
                        ValueProvider = new AllPropertiesValueProvider(x),
                        Writable = true
                    })
                    .ToList();
        return props;
    }
}
public class AllPropertiesValueProvider : Newtonsoft.Json.Serialization.IValueProvider
{
    PropertyInfo _propertyInfo;
    public AllPropertiesValueProvider(PropertyInfo p)
    {
        _propertyInfo = p;
    }
    public object GetValue(object target)
    {
        return _propertyInfo.GetValue(target);  //Serialization
    }
    public void SetValue(object target, object value)
    {
        _propertyInfo.SetValue(target, value, null); //Deserialization
    }
}

将属性更改为public。默认情况下,它不反序列化非公共属性

internal class reCaptchaResponse
{
    public bool success { get; set; }
    public DateTime challenge_ts { get; set; }  // timestamp of the challenge load (ISO format yyyy-MM-dd'T'HH:mm:ssZZ)
    public string hostname { get; set; }        // the hostname of the site where the reCAPTCHA was solved
    public string[] error_codes { get; set; }   // optional
}

成员必须是public

internal class reCaptchaResponse
{
    public bool success { get; set; }
public DateTime challenge_ts { get; set; }  // timestamp of the challenge load (ISO format yyyy-MM-dd'T'HH:mm:ssZZ)
    public string hostname { get; set; }        // the hostname of the site where the reCAPTCHA was solved
    public  string[] error_codes { get; set; }   // optional
}

您可以尝试将修饰符从内部更改为公共