1
 public struct bill
{
    public int id;
    public DateTime docdate;
    public ArrayList billDetails;//this will point the billitems structure
};
public struct billItems
{
    public int id;
    public int item;
    public int quantity;
    public string name;
    public double price;
    public ArrayList tax;
    public double discount;
  };
public ArrayList ReadBill(MySqlConnection connection)
    {
        
            ArrayList billList = new ArrayList( );
            ArrayList billdetailsList = new ArrayList();
            ArrayList details = new ArrayList();
            bill bills = new bill();
            DataTable dt = new DataTable();
            MySqlCommand cmd = new MySqlCommand("SELECT id,DATE_FORMAT(DATE, \"%d-%m-%Y\")docdate,contents FROM bills WHERE progress='Paid'; ", connection);
            dt.Load(cmd.ExecuteReader());
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                bills.id = Convert.ToInt32(dt.Rows[i]["id"].ToString());
                bills.docdate =Convert.ToDateTime(dt.Rows[i]["docdate"].ToString());
                bills.billDetails = new ArrayList();
                bills.billDetails = JsonConversion(dt.Rows[i]["contents"].ToString());//function to get deserialization of json string
                string str = "";
                foreach (var items in bills.billDetails)
                {
                    str= items.ToString();
                    string replacement = Regex.Replace(str, @"\t|\n|\r", "");//removing the spaces
                    replacement = Regex.Replace(replacement, " ", "");
                    details.Add(replacement);  //adding to arraylist 
                }
                bills.billDetails = details;// mapping arraylist to structure variable arraylist
//i want to assign this arralist to a structure which contains the details of billitems.
 }

    private ArrayList JsonConversion(string v)
    {
        int len;
        ArrayList testarray = new ArrayList();
        ArrayList resultarraylist = new ArrayList();
        testarray= JsonConvert.DeserializeObject<ArrayList>(v);
        string array = "";
        char[] splitter_securityIDs = { ',' };
        foreach (var items in testarray)
        {
                          
            array = items.ToString();
            array = array.Remove(0, 1);
            len = array.Length;
            array = array.Remove(len - 1, 1);
            resultarraylist.AddRange(array.Split(splitter_securityIDs));
            return resultarraylist;
        }
        return null;
    }
    

**i have two structure bill and billitems. bill have billdetails arraylist which will point to billitems. i got a json string from the mysql db using the select query. i got the json string Like below for a recod.

[{"id":46,"item":1,"quantity":10,"name":"GARLIC CHEESE ROLL ","price":"50.000","tax":{"name":null,"price":50,"quantity":10,"percent":0,"amount":0,"contents":[]},"discount":0},{"id":47,"item":85,"quantity":1,"name":"PISTA SCOOPS","price":"70.000","tax":{"name":null,"price":70,"quantity":1,"percent":0,"amount":0,"contents":[]},"discount":0}]

enter image description here **this is the datatable i get after select query.

now i want to split this details ("id":46,"item":1,"quantity":10,"name":"GARLIC CHEESE ROLL ","price":"50.000","tax":{"name":null,"price":50,"quantity":10,"percent":0,"amount":0,"contents":[]},"discount":0) to the structure called billitems.

Plz help me in this. thanks in advance.

Mousmi
  • 11
  • 1
  • 1
    Instead of writting your own deserializer use newtonsoft nuget to do it for you: https://www.newtonsoft.com/json/help/html/DeserializeObject.htm – Connor Stoop Mar 30 '21 at 09:30
  • 1
    I second the suggestion to use newtonsoft json.Net. I would also recommend to avoid `ArrayList`, use a typed collection instead, like `billItems[]` or `List`. This will make it possible for the serializer to figure out the types correctly. – JonasH Mar 30 '21 at 09:39
  • I will drop every ArrayList and struct. Simply past a valid json in any tool that will generate the class for you. Like https://app.quicktype.io. The the whole code will be `JsonConvert.DeserializeObject(json)` . And boom all data populated :) – Self Mar 30 '21 at 09:46
  • Does this answer your question? [How to auto-generate a C# class file from a JSON string](https://stackoverflow.com/questions/21611674/how-to-auto-generate-a-c-sharp-class-file-from-a-json-string) – Self Mar 30 '21 at 09:46
  • And [How can I parse JSON with C#?](https://stackoverflow.com/questions/6620165/how-can-i-parse-json-with-c) . – Self Mar 30 '21 at 09:49
  • i want in structure and arraylist. – Mousmi Mar 30 '21 at 09:55

0 Answers0