-2

Im trying to parse a string

{"Url":"http://repreeapi.cloudapp.net/PublicApi/{ActionName}/f23284d5-90a7-4c41-9bd4-8a47e64b4a75"}

I would just like to keep this part and save it as a new string: http://repreeapi.cloudapp.net/PublicApi/{ActionName}/f23284d5-90a7-4c41-9bd4-8a47e64b4a75

And then I would like to replace {ActionName} with "launch"

so the final string should be

http://repreeapi.cloudapp.net/PublicApi/launch/f23284d5-90a7-4c41-9bd4-8a47e64b4a75

I've tried using the split method but can't seem to get the result I want. Any help would be appreciated?

chillax786
  • 369
  • 4
  • 14
  • And when `split` didn't work you just decided to ask here without trying anything else? – Eser Feb 29 '16 at 21:19
  • The original data looks a lot like json, so try to use a json library to parse it, e.g. json.net. With the original one parsed, the ActionName replace can be a simple String.Replace() call. – Evert Feb 29 '16 at 21:19
  • Possible duplicate of [How can I parse JSON with C#?](http://stackoverflow.com/questions/6620165/how-can-i-parse-json-with-c) – Rob Mar 01 '16 at 04:15

2 Answers2

3

As suggested in my comment, you can use json.net, e.g.:

using Newtonsoft.Json;
using System;
class Program
{
  class Wrapper
  {
    public string Url { get; set; }
  }

  static void Main(string[] args)
  {
    Wrapper data = JsonConvert.DeserializeObject<Wrapper>("{\"Url\":\"http://repreeapi.cloudapp.net/PublicApi/{ActionName}/f23284d5-90a7-4c41-9bd4-8a47e64b4a75\"}");
    string url = data.Url.Replace("{ActionName}", "launch");
    Console.WriteLine(url);
  }
}
Evert
  • 354
  • 2
  • 7
  • Actually I'm getting the string from an REST request I make and the response I just save to a String as such: String mystr = response.content(); And then when I do Console.WriteLine(mystr) I see the string displayed as {"Url":"http://repreeapi.cloudapp.net/PublicApi/{ActionName}/f23284d5-90a7-4c41-9bd4-8a47e64b4a75"} – chillax786 Feb 29 '16 at 21:38
  • 1
    @hasanqureshi That still does not change the answer, instead of a hardcoded string you feed in the string from the result of the REST request in to DeserializeObject – Scott Chamberlain Feb 29 '16 at 21:39
-1
        string s = "{\"Url\":\"http://repreeapi.cloudapp.net/PublicApi/{ActionName}/f23284d5-90a7-4c41-9bd4-8a47e64b4a75\"}";
        // Get the URL - 3 element if split by double quotes
        string sURL = s.Split('"')[3];
        // Now replace the "{ActionName}" with something else
        string sURL2 = sURL.Replace("{ActionName}", "launch");
SashaDu
  • 376
  • 1
  • 5