2016-02-29 33 views
-2

我试着解析字符串字符串解析在C#中的HTTP链接

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

我只是想保持此部分并将其保存为一个新的字符串: http://repreeapi.cloudapp.net/PublicApi/ {ActionName}/f23284d5-90a7-4c41-9bd4-8a47e64b4a75

然后,我想用“推出”

所以更换{ActionName}最后的字符串应该是

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

我已经使用分割方法试过,但似乎无法得到我想要的结果。任何帮助,将不胜感激?

+0

当'split'没有工作,你就决定在这里问不尝试别的东西吗? – Eser

+0

原始数据看起来很像json,所以尝试使用json库来解析它,例如, json.net。在解析原始分析后,ActionName替换可以是一个简单的String.Replace()调用。 – Evert

+0

可能的重复[我如何解析JSON与C#?](http://stackoverflow.com/questions/6620165/how-can-i-parse-json-with-c) – Rob

回答

3

如我的评论所说,你可以使用json.net,例如:

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); 
    } 
} 
+0

其实我得到的字符串从我做的一个REST请求和我保存到String的响应,如下所示:String mystr = response.content();然后当我做Console.WriteLine(mystr)时,我看到字符串显示为{“Url”:“http://repreeapi.cloudapp.net/PublicApi/{ActionName}/f23284d5-90a7-4c41-9bd4-8a47e64b4a75”} – chillax786

+1

@hasanqureshi这仍然不会改变答案,而不是你从REST请求的结果中提取字符串的硬编码字符串到DeserializeObject –

-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"); 
+0

看起来它对我有用,它不需要额外的库。 – kemiller2002