C#Web Api获取POST json作为自定义字符串类型
通过C#Web Api获取POST json作为自定义字符串类型,c#,json,string,asp.net-web-api,put,C#,Json,String,Asp.net Web Api,Put,通过Put和Get请求,我收到一个应该包含json字符串的字段的空字符串 例如: 我提出了以下建议: { "card": { "foo": "bar", "xyz": "dby" } } 现在,如果接收类有一个JObject类型的成员,那么它就被正确映射了 public class contact { public int id { get; set; } public string name { get; set; } public JO
Put
和Get
请求,我收到一个应该包含json字符串的字段的空字符串
例如:
我提出了以下建议:
{
"card": {
"foo": "bar",
"xyz": "dby"
}
}
现在,如果接收类有一个JObject类型的成员,那么它就被正确映射了
public class contact {
public int id { get; set; }
public string name { get; set; }
public JObject card { get; set; }
}
但是,如果我将类型更改为可以接收所有JToken的自定义字符串类型:
public class contact {
public int id { get; set; }
public string name { get; set; }
public JsonString card { get; set; } //**Changed HERE**//
}
然后,Put和Get方法都显示空字符串
JsonString如下所示:
public class JsonString
{
private string _json;
public JsonString (string json)
{
this._json = json;
}
public string Value()
{
return _json;
}
public override int GetHashCode()
{
return _json.GetHashCode();
}
public override bool Equals(object obj)
{
return (obj is JsonString) && this.GetHashCode() == obj.GetHashCode();
}
public override string ToString()
{
return _json;
}
}
jsonString类更多,但在此上下文中可能不需要。知道为什么put/Get返回空字符串吗?我该如何修复它
编辑:
Put看起来像这样:
[HttpPut]
[Route("contacts/{contactid}")]
public HttpResponseMessage update(int id, contact c) {
contact.update(c);
}
JObject看起来像这样:
public class JObject : JContainer, IDictionary<string, JToken>,
ICollection<KeyValuePair<string, JToken>>, IEnumerable<KeyValuePair<string, JToken>>,
IEnumerable, INotifyPropertyChanged, ICustomTypeDescriptor, INotifyPropertyChanging
公共类JObject:JContainer,IDictionary,
i集合,i可数,
IEnumerable,INotifyPropertyChanged,ICustomTypeDescriptor,INotifyPropertyChanged
您得到的只是一个字符串,而JObject充当一个键值datastucture
你不能像这样绘制地图。
使用dynamic object可能会有更好的运气,但它仍然没有那么简单。JObject如下所示:
public class JObject : JContainer, IDictionary<string, JToken>,
ICollection<KeyValuePair<string, JToken>>, IEnumerable<KeyValuePair<string, JToken>>,
IEnumerable, INotifyPropertyChanged, ICustomTypeDescriptor, INotifyPropertyChanging
公共类JObject:JContainer,IDictionary,
i集合,i可数,
IEnumerable,INotifyPropertyChanged,ICustomTypeDescriptor,INotifyPropertyChanged
您得到的只是一个字符串,而JObject充当一个键值datastucture
你不能像这样绘制地图。
使用动态对象可能会更幸运,但仍然没有那么简单。这并不是对您的问题的真正回应,但如果您有时间,可以看看Newtonsoft.Json,它很容易使用,并为您提供了正确的序列化和反序列化对象到Json和向后的功能。您在应用程序中到底想做什么
JsonString
?这并不是对您的问题的真正回应,但如果您有时间,可以看看Newtonsoft.Json,它很容易使用,并为Json对象和向后对象提供正确的序列化和反序列化。您在JsonString
中到底想做什么?