谁能建议如何在使用查询匹配调用 Get API 时动态生成 Rest body
Can anyone suggest how to generate Rest body dynamically while calling Get API with Query match
就我而言,在调用 API(GET 方法)时,我应该将以下查询作为正文的一部分发送。
URI : GET XXX/_search
Body:
{
"query":
{
"match_all":
{
}
}
}
截至目前,我正在对字符串进行硬编码并将其附加到资源主体。
希望下面的代码片段能帮到您。
消费者:
JsonField jsn1 = new JsonField() { Name = "_id"};
jsn1.AddValue("1234");
JsonField jsn2 = new JsonField() { Name = "_name" };
jsn2.AddValue("CDE");
JsonField jsn0 = new JsonField(true) { Name = "match" };
JsonField jsn = new JsonField(true) { Name = "query" };
JsonConverter.MapField(jsn0, jsn1);
JsonConverter.MapField(jsn, jsn0);
JsonConverter jsonconverter = new JsonConverter();
jsonconverter.Add(jsn);
JsonField jsn3 = new JsonField() { Name = "name" };
jsn3.AddValue("temp");
jsonconverter.Add(jsn3);
Console.WriteLine(jsonconverter.GetJsonFormat());
检查下面的逻辑。
public class JsonConverter
{
LinkedList<JsonField> jsonlinking = new LinkedList<JsonField>();
public void Add(JsonField jsonfield)
{
LinkedListNode<JsonField> jsonelement = jsonlinking.Last;
if (jsonelement != null)
{
jsonelement.Value.SpecialChar = ",";
}
jsonlinking.AddLast(jsonfield);
}
public string GetJsonFormat()
{
jsonformat = string.Empty;
jsonformat += "{" + Environment.NewLine;
foreach (var item in jsonlinking)
{
ReadJson(item);
}
jsonformat += Environment.NewLine + "}" + Environment.NewLine;
return jsonformat;
}
public static void MapField(JsonField primary, JsonField relative)
{
primary.Next = relative;
}
public static void MapField(Queue<JsonField> linksource)
{
JsonField primary = null;
JsonField relative = null;
foreach (var item in linksource)
{
if (primary == null)
{
primary = item;
}
else if (relative == null)
{
relative = null;
}
else
{
JsonConverter.MapField(primary, relative);
primary = null;
relative = null;
}
}
}
private string jsonformat = string.Empty;
private void ReadJson(JsonField field)
{
if (field != null)
{
jsonformat += @"""";
jsonformat += field.Name;
jsonformat += @"""";
if (field.isContainer)
{
jsonformat += @":{" + Environment.NewLine;
int count = field.ChildNodes.Count();
if (count > 0)
{
foreach (var item in field.ChildNodes)
{
ReadJson(item);
}
}
jsonformat = (jsonformat.Substring(jsonformat.Length - 1, 1) == ",") ? jsonformat.Substring(0, jsonformat.Length - 1) : jsonformat;
jsonformat += @"}" + field.SpecialChar + Environment.NewLine;
}
else
{
jsonformat += @":";
jsonformat += field.GetValues();
}
}
}
}
public class JsonField
{
private int Size = 1;
private int CurrentIndex = 0;
public string Name { get; set; }
private string[] _value;
public string[] Value { get { return _value; } private set { _value = value; } }
public bool isContainer{get;set;}
public JsonField()
{
this.Value = new string[this.Size];
}
private Queue<JsonField> _next = new Queue<JsonField>();
public JsonField Next {
set
{
if (this._next.Count>0)
{
foreach (var item in this._next)
{
item.SpecialChar = ",";
}
}
this._next.Enqueue(value);
}
}
public IEnumerable<JsonField> ChildNodes { get { return this._next; } }
public JsonField(int valuesize)
{
this.Size = valuesize;
this.Value = new string[this.Size];
}
public JsonField(bool iscontainer)
{
this.isContainer = iscontainer;
}
public void AddValue(string value)
{
if (CurrentIndex >= this.Value.Length)
{
throw new ArgumentException("Index Out of Range over Value Array");
return;
}
this.Value[CurrentIndex] = value;
CurrentIndex++;
}
public void ClearValue()
{
this.Value = null;
this.Value = new string[this.Size];
}
public string GetValues()
{
if (this.Value.Length == 1)
{
return @"""" + this.Value[0] + @"""";
}
string returns=string.Empty;
for (int index = 0; index < this.Value.Length; index++)
{
returns += @"""" + this.Value[index] + @"""";
if ((index +1) < this.Value.Length)
{
returns += ",";
}
}
return "[" + returns+ "]";
}
public string SpecialChar { get; set; }
}
就我而言,在调用 API(GET 方法)时,我应该将以下查询作为正文的一部分发送。
URI : GET XXX/_search
Body:
{
"query":
{
"match_all":
{
}
}
}
截至目前,我正在对字符串进行硬编码并将其附加到资源主体。
希望下面的代码片段能帮到您。 消费者:
JsonField jsn1 = new JsonField() { Name = "_id"};
jsn1.AddValue("1234");
JsonField jsn2 = new JsonField() { Name = "_name" };
jsn2.AddValue("CDE");
JsonField jsn0 = new JsonField(true) { Name = "match" };
JsonField jsn = new JsonField(true) { Name = "query" };
JsonConverter.MapField(jsn0, jsn1);
JsonConverter.MapField(jsn, jsn0);
JsonConverter jsonconverter = new JsonConverter();
jsonconverter.Add(jsn);
JsonField jsn3 = new JsonField() { Name = "name" };
jsn3.AddValue("temp");
jsonconverter.Add(jsn3);
Console.WriteLine(jsonconverter.GetJsonFormat());
检查下面的逻辑。
public class JsonConverter
{
LinkedList<JsonField> jsonlinking = new LinkedList<JsonField>();
public void Add(JsonField jsonfield)
{
LinkedListNode<JsonField> jsonelement = jsonlinking.Last;
if (jsonelement != null)
{
jsonelement.Value.SpecialChar = ",";
}
jsonlinking.AddLast(jsonfield);
}
public string GetJsonFormat()
{
jsonformat = string.Empty;
jsonformat += "{" + Environment.NewLine;
foreach (var item in jsonlinking)
{
ReadJson(item);
}
jsonformat += Environment.NewLine + "}" + Environment.NewLine;
return jsonformat;
}
public static void MapField(JsonField primary, JsonField relative)
{
primary.Next = relative;
}
public static void MapField(Queue<JsonField> linksource)
{
JsonField primary = null;
JsonField relative = null;
foreach (var item in linksource)
{
if (primary == null)
{
primary = item;
}
else if (relative == null)
{
relative = null;
}
else
{
JsonConverter.MapField(primary, relative);
primary = null;
relative = null;
}
}
}
private string jsonformat = string.Empty;
private void ReadJson(JsonField field)
{
if (field != null)
{
jsonformat += @"""";
jsonformat += field.Name;
jsonformat += @"""";
if (field.isContainer)
{
jsonformat += @":{" + Environment.NewLine;
int count = field.ChildNodes.Count();
if (count > 0)
{
foreach (var item in field.ChildNodes)
{
ReadJson(item);
}
}
jsonformat = (jsonformat.Substring(jsonformat.Length - 1, 1) == ",") ? jsonformat.Substring(0, jsonformat.Length - 1) : jsonformat;
jsonformat += @"}" + field.SpecialChar + Environment.NewLine;
}
else
{
jsonformat += @":";
jsonformat += field.GetValues();
}
}
}
}
public class JsonField
{
private int Size = 1;
private int CurrentIndex = 0;
public string Name { get; set; }
private string[] _value;
public string[] Value { get { return _value; } private set { _value = value; } }
public bool isContainer{get;set;}
public JsonField()
{
this.Value = new string[this.Size];
}
private Queue<JsonField> _next = new Queue<JsonField>();
public JsonField Next {
set
{
if (this._next.Count>0)
{
foreach (var item in this._next)
{
item.SpecialChar = ",";
}
}
this._next.Enqueue(value);
}
}
public IEnumerable<JsonField> ChildNodes { get { return this._next; } }
public JsonField(int valuesize)
{
this.Size = valuesize;
this.Value = new string[this.Size];
}
public JsonField(bool iscontainer)
{
this.isContainer = iscontainer;
}
public void AddValue(string value)
{
if (CurrentIndex >= this.Value.Length)
{
throw new ArgumentException("Index Out of Range over Value Array");
return;
}
this.Value[CurrentIndex] = value;
CurrentIndex++;
}
public void ClearValue()
{
this.Value = null;
this.Value = new string[this.Size];
}
public string GetValues()
{
if (this.Value.Length == 1)
{
return @"""" + this.Value[0] + @"""";
}
string returns=string.Empty;
for (int index = 0; index < this.Value.Length; index++)
{
returns += @"""" + this.Value[index] + @"""";
if ((index +1) < this.Value.Length)
{
returns += ",";
}
}
return "[" + returns+ "]";
}
public string SpecialChar { get; set; }
}