【问题标题】:How to convert two table data in json formate如何将两个表格数据转换成json格式
【发布时间】:2019-09-10 18:05:46
【问题描述】:

我想要这种格式的 JSON 数据。 itemlist 数据来自 itemlist 表,其余来自另一个表。itemlist 是第二个表名。

JSON

  { 
       \"supplyType\":\"O\",
       \"subSupplyType\":\"1\",
       \"subSupplyDesc\":\"\",
       \"docType\":\"INV\",
       \"docNo\":\"107500F18TO0045\",
       \"docDate\":\"15/10/2017\",
       \"fromGstin\":\"03EHFPS5910D3A1\",
       \"fromTrdName\":\"Diwa\",
       \"fromAddr1\":\"2ND CROSS NO 59 19 \",
       \"fromAddr2\":\"GROUND FLOOR OSBORNE ROAD \",
       \"fromPlace\":\"BANGALORE\",
       \"fromPincode\":560042,
       \"actFromStateCode\":29,
       \"fromStateCode\":29,
       \"toGstin\":\"02EHFPS10D2Z0\",
       \"toTrdName\":\"sthuthya\",
       \"toAddr1\":\"Shree Nilaya\",
       \"toAddr2\":\"Dasarahosahalli\",
       \"toPlace\":\"Beml Nagar\",
       \"toPincode\":400013,
       \"actToStateCode\":9,
       \"toStateCode\":27,
       \"transactionType\":4,
       \"dispatchFromGSTIN\":\"29AAAAA1303P1ZV\",
       \"dispatchFromTradeName\":\"xyz Traders\",
       \"shipToGSTIN\":\"03EHFPS5910D3A1\",
       \"shipToTradeName\":\"XYZ Traders\",
       \"otherValue\":-100,
       \"totalValue\":100,
       \"cgstValue\":0,
       \"sgstValue\":0,
       \"igstValue\":300.67,
       \"cessValue\":400.56,
       \"cessNonAdvolValue\":400,
       \"totInvValue\":68358,
       \"transporterId\":\"\",
       \"transporterName\":\"\",
       \"transDocNo\":\"\",
       \"transMode\":\"1\",
       \"transDistance\":\"656\",
       \"transDocDate\":\"\",
       \"vehicleNo\":\"PVC1234\",
       \"vehicleType\":\"R\",
       \"itemList\":[ 
          { 
             \"productName\":\"rice\",
             \"productDesc\":\"Wheat\",
             \"hsnCode\":1001,
             \"quantity\":4,
             \"qtyUnit\":\"BOX\",
             \"cgstRate\":0,
             \"sgstRate\":0,
             \"igstRate\":3,
             \"cessRate\":0,
             \"cessNonAdvol\":0,
             \"taxableAmount\":56099
          }
       ]
    }";

//

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

          Dictionary<string, object> rowelement;
        [System.Web.Http.HttpGet]
        public JsonResult Show()
        {
            JavaScriptSerializer serial1 = new JavaScriptSerializer();
            Registration obj = new Registration();
            DataTable dt = new DataTable();
            dt = obj.employeedetails();   
            if (dt.Rows.Count > 0) 
            {
                foreach (DataRow dr in dt.Rows)
                {
                    rowelement = new Dictionary<string, object>();
                    foreach (DataColumn col in dt.Columns)
                    {
                        rowelement.Add(col.ColumnName, dr[col]); 
                    }
                    rows.Add("",rowelement);
                }

            }

            return  Json(rows, JsonRequestBehavior.AllowGet);

【问题讨论】:

  • 创建一个自定义的 JsonConverter,它将读取表格并为您创建结构。C# JSON custom serialization 或创建类似于您的 JSON 对象的类结构并添加 JSON 属性属性以定义自定义属性名称。跨度>

标签: c#


【解决方案1】:

这只是一种方法。不是一个完整的解决方案。

您可以创建一个新类,在其中填充来自 DB 的数据,使用 JSON 库对其进行序列化。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    • 2021-11-14
    • 1970-01-01
    • 2014-02-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多