locked
ASK - forech array in asp mvc using SPLIT and return value using json RRS feed

  • Question

  • User-519408813 posted

    IN MY CONTROLLER

    data = 1:B,2:C,3:D,4:A,5:D (Result form database)
    var data1 = data.Split(',');
    List<String> opsi = new List<String>();
    foreach (string da1 in data1) {
    string[] data2 = da1.Split(':');
    opsi.Add(data2[0]);
    }
    return Json(new { info = form["jml_soal"].ToString(), data = opsi });

    --BUT RETURN LIKE 1,2,3,4,5--

    I WANT RETURN LIKE B,C,D,A,D

    PLISS HELP

    Tuesday, May 12, 2020 10:24 PM

Answers

  • User-1330468790 posted

    Hi Sepjak,

     

    It seems like you are adding a wrong value into the variable "opsi". 

    The correct way should be as below :

    data = 1:B,2:C,3:D,4:A,5:D (Result form database)
    var data1 = data.Split(',');
    List<String> opsi = new List<String>();
    foreach (string da1 in data1) {
    string[] data2 = da1.Split(':');  // For example, for "1:B", data2[0] will be "1" and data2[1] will be "B"
    opsi.Add(data2[1]);
    }
    return Json(new { info = form["jml_soal"].ToString(), data = opsi });

     

    Hope this can help you.

    Best regards,

    Sean

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 13, 2020 1:42 AM

All replies

  • User303363814 posted

    opsi.Add(data2[1]);

    Or in one line of code

    return Json(new {info=whatever, data = data.Split(',').Select(d => d.Split(':')[1]) });

    Wednesday, May 13, 2020 1:41 AM
  • User-1330468790 posted

    Hi Sepjak,

     

    It seems like you are adding a wrong value into the variable "opsi". 

    The correct way should be as below :

    data = 1:B,2:C,3:D,4:A,5:D (Result form database)
    var data1 = data.Split(',');
    List<String> opsi = new List<String>();
    foreach (string da1 in data1) {
    string[] data2 = da1.Split(':');  // For example, for "1:B", data2[0] will be "1" and data2[1] will be "B"
    opsi.Add(data2[1]);
    }
    return Json(new { info = form["jml_soal"].ToString(), data = opsi });

     

    Hope this can help you.

    Best regards,

    Sean

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 13, 2020 1:42 AM
  • User-474980206 posted

    or simplify:

    var data = "1:B,2:C,3:D,4:A,5:D";
    var opsi = data.Split(',').Select(e => e.Split(':')[1]);
    
    return Json(new { info = form["jml_soal"].ToString(), data = opsi });
    

    Wednesday, May 13, 2020 3:15 PM
  • User-519408813 posted

    Yes bro..but i get message error LIke this

    System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'

    Wednesday, May 13, 2020 7:39 PM
  • User-474980206 posted

    that's means some of your elements do not have a ":" if you know the default you want, you can:

    var opsi = data.Split(',').Select(e => e.Split(':').ElementAtOrDefault(1) ?? "missing value");
    

    Wednesday, May 13, 2020 8:37 PM
  • User303363814 posted

    Yes bro..but i get message error LIke this

    System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'

    The data coming from the database is not in the form that you think it is.   You need to validate the data before it is stored in the database and check it when it comes out.

    (Not really relevant, this is a bad database design because you are not in first normal form

    Wednesday, May 13, 2020 11:15 PM
  • User-1330468790 posted

    Hi Sepjak,

     

    Could you please check the data from database?

    One of reasons is that the data is not like what you said "1:B,2:C,3:D,4:A,5:D" but something like "1,2:C,3,4:A,5:D".

    That way, when you split each string in the list, for example, "1", you will get an array string[] "data2whose length is 1 and you can access the only element in array like "data2[0]".

    Another possible reason is that you use a different delimiter other than "," and ":".When you are using these delimiter to split the string "data", you might not be able to get a string list as you expected.

     

    Best regards,

    Sean

    Thursday, May 14, 2020 2:12 AM
  • User-519408813 posted

    Thanks All

    Friday, May 15, 2020 10:32 PM