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)
返回
{}
答案 0 :(得分:5)
默认情况下,Json.Net仅序列化/反序列化公共文件和属性,但您也可以在不更改从内部到公共的访问修饰符的情况下执行此操作。
只需使用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
}
}
答案 1 :(得分:3)
将属性更改为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
}
答案 2 :(得分:1)
成员必须是公开的
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
}
答案 3 :(得分:0)
您可以尝试将修改器从内部更改为公共