用于 http 链接的 c# 字符串分析
本文关键字:字符串 http 链接 用于 | 更新日期: 2023-09-27 18:36:50
我试图解析字符串
{"url":"http://repreeapi.cloudapp.net/PublicApi/{ActionName}/f23284d5-90a7-4c41-9bd4-8a47e64b4a75"}
我只想保留这部分并将其另存为新字符串:http://repreeapi.cloudapp.net/PublicApi/{动作名称}/f23284d5-90a7-4c41-9bd4-8a47e64b4a75
然后我想用"启动"替换 {ActionName}
所以最后一个字符串应该是
http://repreeapi.cloudapp.net/PublicApi/launch/f23284d5-90a7-4c41-9bd4-8a47e64b4a75
我尝试使用拆分方法,但似乎无法获得我想要的结果。任何帮助将不胜感激?
正如我的评论中所建议的,您可以使用 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);
}
}
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");