locked
Using convert match collection to list string join in c# RRS feed

  • Question

  • User1151703306 posted

    Hi all,

    I need your appreciated help.

    This is my initial string

    string val = [{"id":4084,"name":"mrrame@gmail.com"},{"id":4085,"name":"mrgio@yahoo.com"},{"id":4083,"name":"mrgar@yahoo.com"}]

    With 

                   JavaScriptSerializer jsSer = new JavaScriptSerializer();
                   object obj = jsSer.DeserializeObject(hidJsonHolder.Value);
    
                    if (obj != null)
                    {
                        Movie[] listMovie = jsSer.ConvertToType<Movie[]>(obj);
                        foreach (Movie p in listMovie)
                        {
                            string pattern = @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
                            Regex re = new Regex(pattern);
    
                            if (p.ToString() != null)
                            {
                                Console.WriteLine("string p = " + p.ToString() + "<br />");
                            }
                        }
                    }

    I got

    string p = [id=4084;name=mrrame@gmail.com]
    string p = [id=4085;name=mrgio@yahoo.com]
    string p = [id=4083;name=mrgar@yahoo.com]

    I can't this return

    mrrame@gmail.com; mrgio@yahoo.com; mrgar@yahoo.com

    I have tried without success using convert match collection to list string join because I need insert into differents MySQL table the string.

    First mail address into dotable_1 and all other mail address into dotable_2.

    On this example:

    1. mrrame@gmail.com, insert into dotable_1;
    2. mrgio@yahoo.com; mrgar@yahoo.com , insert into dotable_2

    but I don't understand if it's the right way

    My code below

                string val = hidJsonHolder.Value;
                Response.Write("string val = " + val.ToString() + "<br /><br />"); 
    
                JavaScriptSerializer jsSer = new JavaScriptSerializer();
                object obj = jsSer.DeserializeObject(hidJsonHolder.Value);
    
                int count = 0;
                if (obj.GetType().IsArray)
                {
                    count = ((Array)obj).Length;
                }
    
                if (count >= 1)
                {
                    if (obj != null)
                    {
                        Movie[] listMovie = jsSer.ConvertToType<Movie[]>(obj);
                        foreach (Movie p in listMovie)
                        {
                            string pattern = @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
                            Regex re = new Regex(pattern);
    
                            if (p.ToString() != null)
                            {
                                MatchCollection matches = re.Matches(p.ToString());
    
                                string pattern = @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
                                Regex re = new Regex(pattern);
    
                                if (p.ToString() != null)
                                {
                                    MatchCollection matches = re.Matches(p.ToString());
                                    if (matches.Count > 0)
                                    {
                                        for (int i = 0; i < matches.Count; i++)
                                        {
    
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

    Friday, May 15, 2020 5:20 PM

Answers

  • User475983607 posted

    Simplify your code.

        public class EmailModel
        {
            public int id { get; set; }
            public string name { get; set; }
        }
    string val = @"[{ ""id"":4084,""name"":""mrrame@gmail.com""},{ ""id"":4085,""name"":""mrgio@yahoo.com""},{ ""id"":4083,""name"":""mrgar@yahoo.com""}]";
    List<EmailModel> items = JsonConvert.DeserializeObject<List<EmailModel>>(val);
    string value = string.Join(";", items.Select(m => m.name).ToArray());
    Label1.Text = value;

    Frankly, your approach is overly complicated.  Consider learning C# collection basics and generic basics.

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/generics-and-arrays

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 15, 2020 5:52 PM

All replies

  • User475983607 posted

    Simplify your code.

        public class EmailModel
        {
            public int id { get; set; }
            public string name { get; set; }
        }
    string val = @"[{ ""id"":4084,""name"":""mrrame@gmail.com""},{ ""id"":4085,""name"":""mrgio@yahoo.com""},{ ""id"":4083,""name"":""mrgar@yahoo.com""}]";
    List<EmailModel> items = JsonConvert.DeserializeObject<List<EmailModel>>(val);
    string value = string.Join(";", items.Select(m => m.name).ToArray());
    Label1.Text = value;

    Frankly, your approach is overly complicated.  Consider learning C# collection basics and generic basics.

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/generics-and-arrays

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 15, 2020 5:52 PM
  • User1151703306 posted

    Awesome

    thank you for help and suggestion

    have a great week end

                    if (obj != null)
                    {
                        List<EmailModel> items = JsonConvert.DeserializeObject<List<EmailModel>>(val);
                        string value = string.Join(";", items.Select(m => m.name).ToArray());
                        Label1.Text = value;
    
                        string[] values = value.Split(new[] { ';' }, 2);
                        string first = values[0];
    Label2.Text = "FIRST:" + first.ToString(); //insert into dotable_1;
    string rest = values[1];
    Label3.Text = "ALL:" + rest.ToString(); //insert into dotable_2; }

    Friday, May 15, 2020 6:38 PM