繁体   English   中英

使用C#Regex从大字符串中提取特定JSON

[英]Extracting specific JSON out of large string using C# Regex

这是一个示例字符串:

Lorem ipsum dolor就座,ad eam选项suscipit invidunt,iuspropriae detracto cu。 {nc te wisi lo {“ firstName”:“ John”,“ lastName”:“ Doe”} rem,以错误的形式出现{{firstName“:” Anna“,” lastName“:” Smith“} dissentias。 在Omittam Pertinax感官上,pri nihil alterum omittam ad,即vix aperiam sententiae an。 费里(Ferri)指责某人,是多面的tractatos moderatius sea {“ firstName”:“ Peter”,“ lastName”:“ Jones”}。 Mel广告销售utamur,qui utportere 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]"}

这是一个可用来提取值的正则表达式:

({\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
      }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM