[英]How to use Json queries with c# using HttpClient
I am using C# to connect to IBM Cloudant. 我正在使用C#连接到IBM Cloudant。 IBM Cloudant supports JSON queries IBM Cloudant , it is mentioned that I need to use a POST request in order to create a query but it is not explained, I am using HttpClient which has a method PostAsync method.
IBM Cloudant支持JSON查询IBM Cloudant ,提到我需要使用POST请求才能创建查询,但是没有解释,我使用的是HttpClient,它具有方法PostAsync方法。 Does anyone have an idea how to use this method to create a query for example the following query:
有谁知道如何使用此方法创建查询,例如以下查询:
{
"selector": {
"_id": {
"$gt": null
}
}
}
I had some confusion about this as well. 我对此也有些困惑。 Please refer to below.
请参考以下内容。
var client = new HttpClient();
var content = new StringContent("JSON Content");
content.Headers.Add("header-name", "header value");
client.PostAsync("http://example.com/something", content);
Your JSON content can also be a C# object, which you could JSON serialize using something like Newtonsoft.Json 您的JSON内容也可以是C#对象,您可以使用类似Newtonsoft.Json的内容对JSON进行序列化。
You could also try this version, classes for your query 您也可以尝试使用此版本的查询类
public class Id{
public object gt { get; set; }
}
public class Selector{
public Id _id { get; set; }
}
public class RootObject{
public Selector selector { get; set; }
}
serialize tmpObject and PostAsync: 序列化tmpObject和PostAsync:
client.PostAsync(url, new StringContent(tmpObject.ToString(), Encoding.UTF8, "application/json"));
so i finally managed to retrieve the response of the query it is as follows: var jsonString = "{\\"selector\\": {\\"_id\\": {\\"$gt\\": null}},\\"fields\\": [\\"" + Attribute + "\\"],\\"sort\\": [{\\"_id\\": \\"asc\\"}]}"; var content = new StringContent(jsonString, Encoding.UTF8, "application/json"); HttpResponseMessage res = await Client.PostAsync(string.Format("https://{0}.cloudant.com/{1}/_find", User, Database), content); StreamReader streamReader = new StreamReader(await res.Content.ReadAsStreamAsync()); JObject responseContent = (JObject)JToken.ReadFrom(new JsonTextReader(streamReader)); streamReader.Close(); var Elements =responseContent["docs"].Value<JArray>();
因此,我终于设法检索到查询的响应,如下所示:
var jsonString = "{\\"selector\\": {\\"_id\\": {\\"$gt\\": null}},\\"fields\\": [\\"" + Attribute + "\\"],\\"sort\\": [{\\"_id\\": \\"asc\\"}]}"; var content = new StringContent(jsonString, Encoding.UTF8, "application/json"); HttpResponseMessage res = await Client.PostAsync(string.Format("https://{0}.cloudant.com/{1}/_find", User, Database), content); StreamReader streamReader = new StreamReader(await res.Content.ReadAsStreamAsync()); JObject responseContent = (JObject)JToken.ReadFrom(new JsonTextReader(streamReader)); streamReader.Close(); var Elements =responseContent["docs"].Value<JArray>();
var jsonString = "{\\"selector\\": {\\"_id\\": {\\"$gt\\": null}},\\"fields\\": [\\"" + Attribute + "\\"],\\"sort\\": [{\\"_id\\": \\"asc\\"}]}"; var content = new StringContent(jsonString, Encoding.UTF8, "application/json"); HttpResponseMessage res = await Client.PostAsync(string.Format("https://{0}.cloudant.com/{1}/_find", User, Database), content); StreamReader streamReader = new StreamReader(await res.Content.ReadAsStreamAsync()); JObject responseContent = (JObject)JToken.ReadFrom(new JsonTextReader(streamReader)); streamReader.Close(); var Elements =responseContent["docs"].Value<JArray>();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.