Here's a sample string:
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", "lastName":"Smith"}dissentias. At omittam pertinax senserit est, pri nihil alterum omittam ad, vix aperiam sententiae an. Ferri accusam an eos, an facete tractatos moderatius sea{"firstName":"Peter", "lastName":"Jones"}. Mel ad sale utamur, qui ut oportere omittantur, eos in facer ludus dicant.
Assume the following data model exists:
public class Person
{
public string firstName;
public string lastName;
}
How could I use regex to extract JSON out of this text and create a List<Person> with:
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}
Objects can be buried anywhere in the string, so their position relative to words, letters, punctuations, whitespaces, etc. does not matter. If the above JSON notation is broken, simply ignore it. The following would be invalid:
{"firstName":"John", "middleName":"", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith", "age":""},
{"firstName":"Peter", "lastName":"Jones" some text}
In other words, pattern search must be strict to the following:
{"firstName":"[val]", "lastName":"[val]"}