[英]C# Web Api GET POST json as custom string type
With Put
and Get
request I receive empty strings for a field that should contain json string. 使用
Put
和Get
请求,我会收到一个应该包含json字符串的字段的空字符串。
For example: I Put the following Json: 例如:我放下以下Json:
{
"card": {
"foo": "bar",
"xyz": "dby"
}
}
Now if the receiving class has a member of type JObject, then it is mapped correctly 现在,如果接收类具有JObject类型的成员,那么它将被正确映射
public class contact {
public int id { get; set; }
public string name { get; set; }
public JObject card { get; set; }
}
However if I change the type to a custom string type that can receive all JTokens: 但是,如果我将类型更改为可以接收所有JTokens的自定义字符串类型:
public class contact {
public int id { get; set; }
public string name { get; set; }
public JsonString card { get; set; } //**Changed HERE**//
}
Then, the Put and Get method both show empty strings. 然后,Put和Get方法都显示空字符串。
JsonString looks like as follows: 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;
}
}
The jsonString class more, but probably not needed in this context. jsonString类更多,但在此上下文中可能不需要。 Any idea why The put/Get are returning empty strings and how can I fix this?
知道为什么put / Get返回空字符串以及如何解决这个问题?
EDIT: 编辑:
The Put looks like this: Put看起来像这样:
[HttpPut]
[Route("contacts/{contactid}")]
public HttpResponseMessage update(int id, contact c) {
contact.update(c);
}
JObject looks like this: JObject看起来像这样:
public class JObject : JContainer, IDictionary<string, JToken>,
ICollection<KeyValuePair<string, JToken>>, IEnumerable<KeyValuePair<string, JToken>>,
IEnumerable, INotifyPropertyChanged, ICustomTypeDescriptor, INotifyPropertyChanging
What you've got is just a string, while JObject acts as a key-value datastucture. 你得到的只是一个字符串,而JObject充当键值数据结构。
You can't just map it like this. 你不能像这样映射它。 You might have better luck with dynamic object but still it is not that simple.
你可能对动态对象有更好的运气,但仍然不是那么简单。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.