如何以下JSON传递给一个C#打补丁方法w或W / O串行器的Javascript [英] how to pass the following JSON to a C# patch method w or w/o Javascript serializer
问题描述
我工作的一个程序访问的Visual Studio团队服务的REST API(是的Visual Studio在线)。我下面 https://www.visualstudio.com/integrate/api/wit/工作项
I am working on a program to access the REST API for Visual Studio Team Services (was Visual Studio Online). I am following https://www.visualstudio.com/integrate/api/wit/work-items
我可以用这个代码片断传递正确的ID查询工作项目:
I was able to query the work item by passing the correct Id using this code snippet:
var uri = new Uri("https://{instance}.visualstudio.com/DefaultCollection/_apis/wit/workitems/7?api-version=1.0");
GetWorkItem(uri);
public static async void GetWorkItem(Uri uri)
{
try
{
var username = "my username";
var password = " my pass word";
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
Convert.ToBase64String(
ASCIIEncoding.ASCII.GetBytes(
string.Format("{0}:{1}", username, password))));
using (HttpResponseMessage response = client.GetAsync(uri)
.Result)
{
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
}
Console.Read();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Console.Read();
}
}
据这里指定正确返回一个JSON的 https://www.visualstudio.com/integrate/api/wit/work-items# GetalistofworkitemsByIDs
现在我试图更新通过修改其标题的工作项目。
Now I am trying to update the work item by modifying its title .
< A HREF =https://www.visualstudio.com/integrate/api/wit/work-items#UpdateworkitemsUpdateafield相对=nofollow> https://www.visualstudio.com/integrate/api/wit/work-项目#UpdateworkitemsUpdateafield
有关这个我写了一个方法:
For this I wrote a method :
public static async void UpdateWorkItemStatus(Uri requestUri, HttpContent iContent)
{
{
var method = new HttpMethod("PATCH");
var request = new HttpRequestMessage(method, requestUri)
{
Content = iContent
};
HttpResponseMessage response;
try
{
using (HttpClient client = new HttpClient())
{
var username = "my username";
var password = "my password";
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
Convert.ToBase64String(
ASCIIEncoding.ASCII.GetBytes(
string.Format("{0}:{1}", username, password))));
response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
Console.WriteLine(response);
Console.Read();
}
}
catch (TaskCanceledException e)
{
Console.WriteLine("ERROR: " + e.ToString());
Console.Read();
}
}
}
我通过传递的json调用此方法:
I am calling this method by passing my json :
var uri = new Uri("https://{instance}.visualstudio.com/DefaultCollection/_apis/wit/workitems/7?api-version=1.0");
string json = new JavaScriptSerializer().Serialize(new
{
op="replace",
path="fields/System.Title",
value=" 123 New Title"
});
HttpContent httpContent = new StringContent(json, Encoding.UTF8, "application/json-patch+json");
UpdateWorkItemStatus(uri, httpContent);
这是按照的 https://www.visualstudio.com/integrate/api/wit/work-items#Updateworkitems
他们没有任何代码样本,所以我用的JavaScriptSerializer
但是,这并不做任何事情。该代码运行,但没有给出产量和我的工作项目,也不能进行编辑。 。我不知道这是否是格式不正确由于使用的JavaScriptSerializer,但我已经使用这个类之前,它一直很好
They don't have any code samples so I used JavascriptSerializer But this doesn't do anything . The code runs but gives no output and my work item is also not edited. I am not sure if it is incorrect in format due to using JavascriptSerializer but I have used this class before and it has worked well.
基本上我需要通过这个JSON:
Basically I need to pass this JSON :
[
{
"op": "replace",
"path": "fields/System.Title",
"value":"New Title"
}
]
这是如何在正确的格式得到这个运行并通过JSON任何帮助,将不胜感激,即使不使用JS序列化器类。
Any help on how to get this running and pass the JSON in a right format would be appreciated even if without using the JS Serializer class.
最终的想法是将其转换为一种解释脚本,可以在Unix上运行,如卷曲,Python或Perl的。 。上任何指针或建议也将被赞赏
Eventually the idea is to convert this to an interpreted script that can run on Unix, like curl, Python or Perl. Any pointers or recommendations on that would also be appreciated.
推荐答案
我一般直接传递内容的字符串和它的作品:
I usually pass the content strings directly and it works:
string json = "[{\"op\":\"replace\",\"path\":\"/fields/System.Title\",\"value\":\"Title\"}]";
您的JavaScriptSerializer生成的字符串JSON是缺少[和]。
The string json you generated by JavaScriptSerializer is missing "[" and "]".
顺便说一句,你提供的,如果你运行UpdateWorkItemStatus之前GetWorkItem(URI)(URI,httpContent),UpdateWorkItemStatus(代码)将不会因为GetWorkItem后退出应用程序运行( )。
By the way, with the code you provided, if you run GetWorkItem(uri) before UpdateWorkItemStatus(uri, httpContent), UpdateWorkItemStatus() won't run since the app exit after GetWorkItem().
这篇关于如何以下JSON传递给一个C#打补丁方法w或W / O串行器的Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!