I have a string like so:
string newList = "{"data":[{"ours":176,"theirs":"TAMPA","companyId":111},{"ours":176,"theirs":"ORLANDO","companyId":111}]}"
How can I extract the contents into a list<TestClass> allValues = new list<TestClass>()
?
so:
public class TestClass
{
public int Ours { get; set; }
public string Theirs { get; set; }
public int CompanyId { get; set; }
}
So like for exmaple: separate the newList into a list removing {"data":[
and ]}"
I have done this with
string s1 = newList [0..^2];
string AnString = s1[9..];
which then returns
{"ours":176,"theirs":"TAMPA","companyId":111},{"ours":176,"theirs":"ORLANDO","companyId":111}
now normally could do split string by comma but because of the commas inside,
and then do a foreach and populate the list from there...
I also tried this:
var result6 = AnString.Split().Where(x => x.StartsWith("{") && x.EndsWith("}")).ToList();
but that ignored the logic of first '{' and first '}' and took first '{' and last '}' as opposed to first '{' first '}', second '{' second '}' etc...
I know what im doing is serializng and can be done easily with system.text etc... but for the purpose of this exercise im trying to hard code it...
Update forgot to mention cant do it by getting with substring because each variables will have different length. so could be "ours":176
now but next time could be "ours":1221213123121
.