【问题标题】:How to Parse a specific string in C#如何在 C# 中解析特定的字符串
【发布时间】:2016-08-03 09:19:06
【问题描述】:

我有这个结构:

{
   "longUrl" :"http://www.sample.com",
   "bok" :1,
   "url" :"http://pleasegetme.com ",
   "title" :""
}
//equivalent
    "{
       \n   \"longUrl\" :\"http://www.sample.com/\",
       \n   \"bok\" :1,
       \n   \"url\" :\"http://pleasegetme.com \",
       \n   \"title\" :\"\"\n
     }"

我有这个功能

public string Domain1Helper(string longText)
{
    Regex rgxUrl = new Regex("\"url\":\"(.*?)\"");
    Match mUrl = rgxUrl.Match(longText);

    string url = Regex.Replace(mUrl.Groups[1].Value, @"\\", "");
    return url;
}

我想得到的是http://pleasegetme.com

我的Domain1Helper 方法有什么问题?

【问题讨论】:

  • 这是一个 JSON 字符串。使用 json 反序列化器。
  • 不,只是一个字符串。
  • @AJB - 是的,它“只是一个字符串”,而是一个包含 JSON 格式数据的字符串。
  • 正则表达式略有错误。您忘记了 '"url" 中的空格:'
  • @kumarch1,我回滚你的编辑。 OP 不知道它是 JSON(因此在问题“它是 json 字符串”中添加注释是错误的编辑)。我也改变了格式,而你没有。下次编辑时请注意“已更改”警告。

标签: c# json parsing


【解决方案1】:

你有一个 JSON 字符串。您可以使用名为 Json.Net 的库来解析它。你可以找到它作为一个 nuget 包。然后你可以使用下面的代码来挑选你想要的字符串。

JObject jo = JObject.Parse(longtext);
Console.WriteLine(jo["longUrl"].Value.ToString()); // Outputs 'http://www.sample.com'

【讨论】:

    【解决方案2】:

    AJB,您的 RegEx 中有错误。 话虽如此,您应该使用 JSON 反序列化器,例如 JSON.NET。 在函数 Domain1Helper 中应该是:

    Regex rgxUrl = new Regex("\"url\"\\s+:\"(.*?)\"");
    

    注意 \s+ ?

    【讨论】:

      【解决方案3】:

      您已包含转义字符串\。如果你删除这些,它应该解析得很好,如下所示:

      https://regex101.com/r/hK7xR7/1

      注意,你的方法应该是这样的:

      public string Domain1Helper(string longText)
      {
          Regex rgxUrl = new Regex(@"\"url\" :\"(.*?)\"");
          Match mUrl = rgxUrl.Match(longText);
      
          string url = Regex.Replace(mUrl.Groups[1].Value, @"\\", "");
          return url;
      }
      

      另一种解决方案是使用一个 json 库,此处概述:https://msdn.microsoft.com/en-us/library/cc197957(v=vs.95).aspx

      【讨论】:

        【解决方案4】:

        这是一个奇怪的 JSON 格式的字符串。使用 JSON 反序列化器,例如 JSON.NET。我会把它留给你,你想用哪一个。

        这里是使用 json2csharp.com 生成的根对象

        public class RootObject
        {
            public string longUrl { get; set; }
            public int bok { get; set; }
            public string url { get; set; }
            public string title { get; set; }
        }
        

        使用 JSON.NET 的示例:

        using Newtonsoft.Json; // Namespace is something like this
        
        
        string json = "{
                         \n   \"longUrl\" :\"http://www.sample.com/\",
                         \n   \"bok\" :1,
                         \n   \"url\" :\"http://pleasegetme.com \",
                         \n   \"title\" :\"\"\n
                       }";
        
        RootObject rootObject = JsonConvert.DeserializeObject<RootObject>(json);
        string url = rootObject.url;
        // Do something with the url
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-06-27
          • 2019-12-15
          • 2017-09-09
          相关资源
          最近更新 更多