如果 Json 键之一包含点,如何为 Json 序列化声明匿名类型?
How to declare anonymous type for Json serialization, if one of the Json keys contains a dot?
我正在序列化一个匿名对象以用作 HTTP post 请求的请求消息。问题是其中一个 JSON 键的名称中包含一个点。 VS 抛出“'invalid anonymous type member declarator'”错误。
return JsonConvert.SerializeObject(new
{
query = "something",
firstname.keyword = "xyz"
});
我该怎么做才能解决这个问题?
编辑:真正的 json 请求看起来像这样,所以我认为我不能使用字典:
{
"query": {
"bool": {
"must": [
{
"term": {
"firstname.keyword": ""
}
}
],
"must_not": [ ],
"should": [ ]
}
},
"from": 0,
"size": 10,
"sort": [ ],
"aggs": { }
}
Json一般可以用数组、字典和匿名对象来表示
您的 Json 的第一部分可以生成如下:
return JsonConvert.SerializeObject(new
{
query = new
{
@bool = new
{
must = new[]
{
new
{
term = new Dictionary<string, object>
{
["firstname.keyword"] = string.Empty,
}
}
}
}
}
});
我正在序列化一个匿名对象以用作 HTTP post 请求的请求消息。问题是其中一个 JSON 键的名称中包含一个点。 VS 抛出“'invalid anonymous type member declarator'”错误。
return JsonConvert.SerializeObject(new
{
query = "something",
firstname.keyword = "xyz"
});
我该怎么做才能解决这个问题?
编辑:真正的 json 请求看起来像这样,所以我认为我不能使用字典:
{
"query": {
"bool": {
"must": [
{
"term": {
"firstname.keyword": ""
}
}
],
"must_not": [ ],
"should": [ ]
}
},
"from": 0,
"size": 10,
"sort": [ ],
"aggs": { }
}
Json一般可以用数组、字典和匿名对象来表示
您的 Json 的第一部分可以生成如下:
return JsonConvert.SerializeObject(new
{
query = new
{
@bool = new
{
must = new[]
{
new
{
term = new Dictionary<string, object>
{
["firstname.keyword"] = string.Empty,
}
}
}
}
}
});