Home > other >  C# Reading parts of a string into a list
C# Reading parts of a string into a list

Time:05-30

I have the following JSON string:

[
    {
        "Id": 1,
        "UserName": "Test1",
        "UserPassword": "Test1",
        "FirstName": "TF1",
        "LastName": "TL1",
        "Mobile": "Test1",
        "Email": "TE1",
        "CreatedDate": "2022-05-29T00:00:00.000Z"
    },
    {
        "Id": 4,
        "UserName": "Test4",
        "UserPassword": "Test4",
        "FirstName": "T4F",
        "LastName": "TL4",
        "Mobile": "Test4",
        "Email": "TE4",
        "CreatedDate": "2022-05-29T00:00:00.000Z"
    },
    {
        "Id": 3,
        "UserName": "Test3",
        "UserPassword": "Test3",
        "FirstName": "TF3",
        "LastName": "TL3",
        "Mobile": "Test3",
        "Email": "TE3",
        "CreatedDate": "2022-05-29T00:00:00.000Z"
    },
    {
        "Id": 7,
        "UserName": "Test7",
        "UserPassword": "Test7",
        "FirstName": "T7F",
        "LastName": "TL7",
        "Mobile": "Test7",
        "Email": "TE7",
        "CreatedDate": "2022-05-29T00:00:00.000Z"
    },
    {
        "Id": 7,
        "UserName": "Test7",
        "UserPassword": "Test7",
        "FirstName": "T7F",
        "LastName": "TL7",
        "Mobile": "Test7",
        "Email": "TE7",
        "CreatedDate": "2022-05-29T00:00:00.000Z"
    },
    {
        "Id": null,
        "UserName": "PT",
        "UserPassword": "PT",
        "FirstName": null,
        "LastName": null,
        "Mobile": null,
        "Email": null,
        "CreatedDate": null
    },
    {
        "Id": null,
        "UserName": "CTest",
        "UserPassword": "CTest",
        "FirstName": null,
        "LastName": null,
        "Mobile": null,
        "Email": null,
        "CreatedDate": null
    },
    {
        "Id": 5,
        "UserName": "Test5",
        "UserPassword": "Test5",
        "FirstName": "TF5",
        "LastName": "TL5",
        "Mobile": "Test5",
        "Email": "TE5",
        "CreatedDate": "2022-05-29T00:00:00.000Z"
    }
]

And would like to read the all the values of the UserName & UserPassword fields of the string into the following lists:

var UserName = new List<string>();
var UserPassword = new List<string>();

CodePudding user response:

Create a class containing at least those two properties (it could have the others also):

class User
{
    public string UserName;
    public string UserPassword;
}

Parse the JSON using a suitable parser, such as Json.NET

var list = JsonConvert.DeserializeObject<List<User>>(yourJson);

Use Linq to pull out the data you want

var UserName = list.Select(u => u.UserName).ToList();
var UserPassword = list.Select(u => u.UserPassword ).ToList();

dotnetfiddle

CodePudding user response:

From your JSON string

string JSON_String = ...;

You can also extract the data you want with the help of Regex and LINQ. Here is an example

var UserName = Regex
    .Matches(JSON_String, "\"UserName\":\\s*\"\\w \"")
    .Cast<Match>()
    .Select(m => m.Value)
    .Select(s => Regex.Match(s, "\"\\w \"", RegexOptions.RightToLeft).Value)
    .Select(s => s.Substring(1, s.Length - 2))
    .ToList();

var UserPassword = Regex
    .Matches(JSON_String, "\"UserPassword\":\\s*\"\\w \"")
    .Cast<Match>()
    .Select(m => m.Value)
    .Select(s => Regex.Match(s, "\"\\w \"", RegexOptions.RightToLeft).Value)
    .Select(s => s.Substring(1, s.Length - 2))
    .ToList();
  •  Tags:  
  • c#
  • Related