0

I have some data from the bind grid view and I want to display them in a serialized format. Right now I have a nested array where the above parent is serialized correctly but the child array has escape characters and slashes.

private List<string[]> gridDataList = new List<string[]>();

private void BindGridView()
{
    string ss = cb_DOCUMENT_NO.SelectedValue;
    List<Dictionary<string, string>> MainList = new List<Dictionary<string, string>>();

    Dictionary<string, string> rowData = new Dictionary<string, string>();
    rowData["User_id"] = user_id;
    rowData["Region_Id"] = txt_REGION_ID.Text;
    rowData["document_no"] = ss;
    rowData["industry_name"] = cb_INDUSTRY_NAME.SelectedValue;
    rowData["customer_number"] = cb_INDUSTRY_NAME.SelectedValue;
    rowData["posting_date"] = txt_POSTING_DATE.Text;

    List<Dictionary<string, string>> paymentList = new List<Dictionary<string, string>>();

    foreach (GridViewRow row in biGridView.Rows)
    {
        Dictionary<string, string> paymentData = new Dictionary<string, string>();
        for (int i = 0; i < row.Cells.Count; i++)
        {
            string headerText = biGridView.HeaderRow.Cells[i].Text.Trim();
            string cellText = row.Cells[i].Text.Trim();

            if (cellText == "&nbsp;")
            {
                cellText = string.Empty;
            }

            paymentData[headerText] = cellText.Trim();
        }

        paymentList.Add(paymentData);
    }

    rowData["payment_for"] = JsonConvert.SerializeObject(paymentList);
    string jsonData = JsonConvert.SerializeObject(rowData, Formatting.Indented);

    Response.Write("<pre>" + jsonData + "</pre>");
    PostJsonDataInLaravelApp(jsonData);
}

Obtained output:

{
  "User_id": "EAUSER02",
  "Region_Id": "ID-02",
  "document_no": "02-80/81-BIN-00259",
  "industry_name": "02-CNO-0001",
  "customer_number": "02-CNO-0001",
  "posting_date": "03-05-2080",
  "payment_for": "[{\"BillType\":\"गोदाम भवन बहाल\",\"Description\":\"गोदाम भवन बहाल\",\"Amount\":\"1000.00\",\" \":\"\"}]"
}

Expected output:

{
  "User_id": "EAUSER02",
  "Region_Id": "ID-02",
  "document_no": "02-80/81-BIN-00259",
  "industry_name": "02-CNO-0001",
  "customer_number": "02-CNO-0001",
  "posting_date": "03-05-2080",
  "payment_for": [
    {
      "BillType": "गोदाम भवन बहाल",
      "Description": "गोदाम भवन बहाल",
      "Amount": "1000.00",
      " ": ""
    }
  ]
}

1 Answer 1

2

Would say Dictionary<string, string> type is not a good choice. Based on your expected output, you need to store the paymentList as array but not as serialized string.

Instead, you should declare the rowData as Dictionary<string, object> to allow the key-value pair to store the array value.

Dictionary<string, object> rowData = new Dictionary<string, object>();

And you can store the paymentList value as array.

rowData["payment_for"] = paymentList;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.