How to convert one Json data type to another Json data type.

  • Thread starter Thread starter zydjohn
  • Start date Start date
Z

zydjohn

Guest
I have one type of Json data type in string, and I want to convert it to another Json data type using built-in System.Text.Json.

The following is the C# class for Json data type and the source Json string and target Json string.



public class Student
{
public string username { get; set; }
public string run_type { get; set; }
public string school_code { get; set; }
}

public class RootSource
{
public List<Student> Students { get; set; }
}

public class StudentObject
{
public List<List<string>> Students { get; set; }
}

string json_root_source1 = @"{""Students"": [{ ""username"":""John"",""run_type"":""100M"",""school_code"":""ibc""},{ ""username"":""Smith"",""run_type"":""100M"",""school_code"":""sbo""}]}";
string json_root_target1 = @"{""Students"": [{ ""ibc"" : ""John""},{ ""sbo"":""Smith""}]}";


Basically, I want to covert this string

{"Students": [{ "username":"John","run_type":"100M","school_code":"ibc"},{ "username":"Smith","run_type":"100M","school_code":"sbo"}]}";

To this string:

{"Students": [["ibc", "John"],[ "sbo","Smith"]]}

The target string will contain the school_code plus the username.

Then I will use the target string in another more complicate Json data structure.

I have to use .NET core 3.0, with Visual Studio 2019 Version 16.3.10 on Windows 10 Version 1909.

Please advice!

Continue reading...
 
Back
Top