Click here to Skip to main content
14,868,503 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
**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}]

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.

What I have tried:

<pre>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);
            for (int i = 0; i < dt.Rows.Count; i++)
       = 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);
            return resultarraylist;
        return null;
Updated 30-Mar-21 0:11am

1 solution

First off, why are you using ArrayList - it was superseded by Generic collections way back in 2005 with C# V2 ...

Secondly, if I feed your JSON to a converter, I don't get the classes you show:
public class Tax
    public object name { get; set; }
    public int price { get; set; }
    public int quantity { get; set; }
    public int percent { get; set; }
    public int amount { get; set; }
    public List<object> contents { get; set; }

public class Root
    public int id { get; set; }
    public int item { get; set; }
    public int quantity { get; set; }
    public string name { get; set; }
    public string price { get; set; }
    public Tax tax { get; set; }
    public int discount { get; set; }
Particularly relevant is the Tax field which is an ArrayList in your code, and a single item in the JSON.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900