-1

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.

Benjamin Buch
  • 4,752
  • 7
  • 28
  • 51
John
  • 3,965
  • 21
  • 77
  • 163
  • So you want to write a new Json serializer? Why are the existng ones (System.Text.Json and Newtonsoft Json.NET) not suitable for your purposes? – Klaus Gütter Jun 10 '23 at 04:46
  • It’s an exercise, just brushing up on skills. And was curious if there was a way to do this? Thanks for reply – John Jun 10 '23 at 06:11
  • You can start with a JSON grammar (see https://www.json.org/json-en.html) and write a parser for it. There are parser gererators (like ANTLR) or you can do your own parser. Lots of literature for this. I fear SO is not the place to get help for such a task. – Klaus Gütter Jun 10 '23 at 06:29
  • @John If it is exercize why you post it here? It is your exersize, not ours. So do it. All normal people would use a standart json serializer. – Serge Jun 10 '23 at 11:44

2 Answers2

0

Truncating these 2 strings like you have mentioned:

  • start => "{"data":[
  • end => ]}"

This will give you this string:

  • {"ours":176,"theirs":"TAMPA","companyId":111}
  • {"ours":176,"theirs":"ORLANDO","companyId":111}

After that you could use many ways, it depends on if it is an exercise that you should apply some techniques or not:

I hope that the answer gives you some tracks :)

hatem87
  • 157
  • 4
0

Using: https://json2csharp.com/

Gives:

// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
    public class Datum
    {
        public int ours { get; set; }
        public string theirs { get; set; }
        public int companyId { get; set; }
    }

    public class Root
    {
        public List<Datum> data { get; set; }
    }

The code:

using Newtonsoft.Json;

string newList = "{\"data\":[{\"ours\":176,\"theirs\":\"TAMPA\",\"companyId\":111},{\"ours\":176,\"theirs\":\"ORLANDO\",\"companyId\":111}]}";

Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(newList);

List<Datum> l = myDeserializedClass.data.ToList<Datum>();

foreach (Datum item in l)
{
    System.Console.WriteLine($"CompanyId:{item.companyId} ours:{item.ours} theirs:{item.theirs}");
}

produces:

CompanyId:111 ours:176 theirs:TAMPA
CompanyId:111 ours:176 theirs:ORLANDO
Luuk
  • 12,245
  • 5
  • 22
  • 33