使用 C# 正则表达式从大字符串中提取特定 JSON

本文关键字:提取 JSON 字符串 正则表达式 使用 | 更新日期: 2023-09-27 18:32:34

下面是一个示例字符串:

Lorem ipsum dolor sit amet, ad eam option suscipit invidunt, ius Propriae detracto cu.Nec te wisi lo{"firstName":"John", "lastName":"Doe"}rem, in quo vocent erroribus {"firstName":"Anna", "姓氏":"史密斯"}dissentias.At omittam pertinax senserit est, pri 虚无交替 omittam ad, vix aperiam sententiae an.费里控诉安 EOS, an facete tractatos moderatius sea{"firstName":"Peter", "姓氏":"琼斯"}。Mel ad sale utamur, qui ut oportere omittantur, EOS in facer ludus dicant.

假设存在以下数据模型:

public class Person
{
   public string firstName;
   public string lastName;
}

我如何使用正则表达式从此文本中提取 JSON 并使用以下内容创建List<Person>

{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}

对象可以埋在字符串中的任何位置,因此它们相对于单词、字母、标点符号、空格等的位置无关紧要。如果上述 JSON 表示法被破坏,只需忽略它即可。以下内容无效:

{"firstName":"John", "middleName":"", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith", "age":""},
{"firstName":"Peter", "lastName":"Jones" some text}

换句话说,模式搜索必须严格到以下内容:

{"firstName":"[val]", "lastName":"[val]"}

使用 C# 正则表达式从大字符串中提取特定 JSON

下面是一个可用于提取值的正则表达式:

({'s*"firstName":'s*"[^"]+",'s*"lastName":'s*"[^"]+"'s*})

在此之后,我建议只使用 Json.NET 来反序列化对象。

使用此代码片段,

//Take All first Name
    string strRegex1 = @"firstName"":""([^""""]*)"",";
//Take All Last Name
    string strRegex2 = @"lastName"":""([^""""]*)""";
    Regex myRegex = new Regex(strRegex, RegexOptions.None);
   Regex myRegex2 = new Regex(strRegex2, RegexOptions.None);
    string strTargetString = @"{""firstName"":""John"", ""middleName"":"""", ""lastName"":""Doe""}," + "'n" + @"{""firstName"":""Anna"", ""lastName"":""Smith"", ""age"":""""}," + "'n" + @"{""firstName"":""Peter"", ""lastName"":""Jones"" some text}";
    foreach (Match myMatch in myRegex.Matches(strTargetString))
    {
      if (myMatch.Success)
      {
       // Add your code here for First Name
      }
    }
foreach (Match myMatch in myRegex2.Matches(strTargetString))
    {
      if (myMatch.Success)
      {
        // Add your code herefor Last Name
      }
    }