locked
Return drop download URL from VSO Rest API RRS feed

  • Question

  • So I am able to pull in a bunch of build info by running the code below.  What code do I need to add to pull the drop download URL from the result?

    using (HttpResponseMessage response = client.GetAsync(
                                    "https://{accountname}.visualstudio.com/DefaultCollection/_apis/build/builds/{buildid}?api-version=1.0-preview.1").Result)
                        {
                            response.EnsureSuccessStatusCode();                     


                            string responseBody = await response.Content.ReadAsStringAsync();
                            Console.WriteLine(responseBody);




    • Edited by cbova Wednesday, October 15, 2014 4:58 PM
    Wednesday, October 15, 2014 2:59 PM

Answers

  • Hello cbova,

    So you've got the returned json response string now? What you want is to retrieve some url information from the json string in C# language, is that right?

    In .NET you could use JavascriptSerializer or DataContractJsonSerializer to deserialize the json string back to an object, then get the specified value from the object. The general steps are:

    1. Get the json response string

    2. Create the json object class

    3. Create the json serializer class

    4. Deserialize it to the json object class instance

    5. Get the value you want

    There're already many MSDN document illustrating how to do this:

    http://msdn.microsoft.com/en-us/library/hh674188.aspx

    http://msdn.microsoft.com/en-us/library/cc197957(v=vs.95).aspx

    They're not for VSO REST API only, but the steps are the same.

    If you don't want to create the json object class, you can use some other libraries to help you, for example NewtonSoft.Json, it's pretty easy to get something from the json string dynamically.

    {
    	"uri": "vstfs:///Build/Build/34",
    	"id": 34,
    	"buildNumber": "GitManualBuild_20140714.1",
    	"url": "https://{accountname}.visualstudio.com/DefaultCollection/_apis/build/Builds/34",
    	"startTime": "2014-07-14T07:17:02.303Z",
    	"finishTime": "2014-07-14T07:18:24.24Z",
    	"reason": "manual",
    	"status": "failed",
    	"definition": {
    		"definitionType": "xaml",
    		"id": 3,
    		"name": "GitManualBuild",
    		"url": "https://{accountname}.visualstudio.com/DefaultCollection/_apis/build/Definitions/3"
    	}
    }

    //Get the json string
                var json = System.IO.File.ReadAllText(@"test.json");
                //Parse it to json object
                dynamic jsonObject = JObject.Parse(json);
                //Get the specified value
                dynamic definition_url = jsonObject.definition.url;
    
                Console.WriteLine(definition_url);

    The above code shows to you how to use NewtonSoft.Json to get the definition url of the json string. Of course you need to install the library from Nuget.

    Another approach is to use Regular Expression to search the json string and use some pattern to get the matched url string. Search "c# regular expression", and you'll find a bunch of answers. For example:

    How to: Search Strings Using Regular Expressions (C# Programming Guide)


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Edited by Caillen Thursday, October 16, 2014 3:15 AM
    • Marked as answer by Caillen Wednesday, October 22, 2014 1:26 AM
    Thursday, October 16, 2014 3:13 AM