简体   繁体   中英

How to convert Multiple Json object to DataTable or DataSet in C#

I have an below json object.I would to know 2 points

  1. How to convert below json object to DataTable or Dataset in C# Code
  2. After converting,reverse back to (How to convert DataTable to Json object) in C# Code. Please provide me example code
[
  {
    "transaction": {
      "Version": "1.01",
      "TranDtls": {
        "TaxSch": "GST",
        "SupTyp": "B2B",
        "RegRev": "Y",
        "EcmGstin": null,
        "IgstOnIntra": "N"
      },
      "DocDtls": {
        "Typ": "INV",
        "No": "DAOC/007",
        "Dt": "10/08/2020"
      },
      "SellerDtls": {
        "Gstin": "29AAFCD5862R000",
        "LglNm": "NIC company pvt ltd",
        "TrdNm": "NIC Industries",
        "Addr1": "5th block, kuvempu layout",
        "Addr2": "kuvempu layout",
        "Loc": "GANDHINAGAR",
        "Pin": 560037,
        "Stcd": "29",
        "Ph": "9000000000",
        "Em": "abc@gmail.com"
      },
      "BuyerDtls": {
        "Gstin": "29AWGPV7107B1Z1",
        "LglNm": "XYZ company pvt ltd",
        "TrdNm": "XYZ Industries",
        "Pos": "12",
        "Addr1": "7th block, kuvempu layout",
        "Addr2": "kuvempu layout",
        "Loc": "GANDHINAGAR",
        "Pin": 562160,
        "Stcd": "29",
        "Ph": "91111111111",
        "Em": "xyz@yahoo.com"
      },
      "DispDtls": {
        "Nm": "ABC company pvt ltd",
        "Addr1": "7th block, kuvempu layout",
        "Addr2": "kuvempu layout",
        "Loc": "Banagalore",
        "Pin": 562160,
        "Stcd": "29"
      },
      "ShipDtls": {
        "Gstin": "29AWGPV7107B1Z1",
        "LglNm": "CBE company pvt ltd",
        "TrdNm": "kuvempu layout",
        "Addr1": "7th block, kuvempu layout",
        "Addr2": "kuvempu layout",
        "Loc": "Banagalore",
        "Pin": 562160,
        "Stcd": "29"
      },
      "ItemList": [
        {
          "SlNo": "1",
          "PrdDesc": "Rice",
          "IsServc": "N",
          "HsnCd": "1001",
          "Barcde": "123456",
          "Qty": 100.345,
          "FreeQty": 10,
          "Unit": "BAG",
          "UnitPrice": 99.545,
          "TotAmt": 9988.84,
          "Discount": 10,
          "PreTaxVal": 1,
          "AssAmt": 9978.84,
          "GstRt": 12.0,
          "IgstAmt": 1197.46,
          "CgstAmt": 0,
          "SgstAmt": 0,
          "CesRt": 5,
          "CesAmt": 498.94,
          "CesNonAdvlAmt": 10,
          "StateCesRt": 12,
          "StateCesAmt": 1197.46,
          "StateCesNonAdvlAmt": 5,
          "OthChrg": 10,
          "TotItemVal": 12897.7,
          "OrdLineRef": "3256",
          "OrgCntry": "AG",
          "PrdSlNo": "12345",
          "BchDtls": {
            "Nm": "123456",
            "ExpDt": "01/08/2020",
            "WrDt": "01/09/2020"
          },
          "AttribDtls": [
            {
              "Nm": "Rice",
              "Val": "10000"
            }
          ]
        }
      ],
      "ValDtls": {
        "AssVal": 9978.84,
        "CgstVal": 0,
        "SgstVal": 0,
        "IgstVal": 1197.46,
        "CesVal": 508.94,
        "StCesVal": 1202.46,
        "Discount": 10,
        "OthChrg": 20,
        "RndOffAmt": 0.3,
        "TotInvVal": 12908,
        "TotInvValFc": 12897.7
      },
      "PayDtls": {
        "Nm": "ABCDE",
        "AccDet": "5697389713210",
        "Mode": "Cash",
        "FininsBr": "SBIN11000",
        "PayTerm": "100",
        "PayInstr": "Gift",
        "CrTrn": "test",
        "DirDr": "test",
        "CrDay": 100,
        "PaidAmt": 10000,
        "PaymtDue": 5000
         },
      "RefDtls": {
        "InvRm": "TEST",
        "DocPerdDtls": {
          "InvStDt": "01/08/2020",
          "InvEndDt": "01/09/2020"
        },
        "PrecDocDtls": [
          {
            "InvNo": "DOC/002",
            "InvDt": "01/08/2020",
            "OthRefNo": "123456"
          }
        ],
        "ContrDtls": [
          {
            "RecAdvRefr": "Doc/003",
            "RecAdvDt": "01/08/2020",
            "Tendrefr": "Abc001",
            "Contrrefr": "Co123",
            "Extrefr": "Yo456",
            "Projrefr": "Doc-456",
            "Porefr": "Doc-789",
            "PoRefDt": "01/08/2020"
          }
        ]
      },
      "AddlDocDtls": [
        {
          "Url": "https://einv-apisandbox.nic.in",
          "Docs": "Test Doc",
          "Info": "Document Test"
        }
      ],
      "ExpDtls": {
        "ShipBNo": "A-248",
        "ShipBDt": "01/08/2020",
        "Port": "INABG1",
        "RefClm": "N",
        "ForCur": "AED",
        "CntCode": "AE"
      },
      "EwbDtls": {
        "TransId": "12AWGPV7107B1Z1",
        "TransName": "XYZ EXPORTS",
        "Distance": 100,
        "TransDocNo": "DOC01",
        "TransDocDt": "10/08/2020",
        "VehNo": "ka123456",
        "VehType": "R",
        "TransMode": "1"
      }
    }
  }
]

You can check it here How to create dataset from Object?

There are a lot of ways you can use to instantiate an object from json string.

If you can't use any kind of third party libraries such as Newtonsoft.Json you can do it with JsonReaderWriterFactory

public static TEntity Create<TEntity>(string json)
{
    using (var memoryStream = new MemoryStream())
    {
        byte[] jsonBytes = Encoding.UTF8.GetBytes(json);
        memoryStream.Write(jsonBytes, 0, jsonBytes.Length);
        memoryStream.Seek(0, SeekOrigin.Begin);
        using (var jsonReader = JsonReaderWriterFactory.CreateJsonReader(
            memoryStream,
            Encoding.UTF8,
            XmlDictionaryReaderQuotas.Max,
            null))
        {
            var     serializer = new DataContractJsonSerializer(typeof(TEntity));
            TEntity entity     = (TEntity)serializer.ReadObject(jsonReader);
            return entity;
        }
    }
}

And vice-versa

public static string Create(object entity)
{
    var serializer = new DataContractJsonSerializer(entity.GetType());

    using (var stream = new MemoryStream())
    {
        using (var writer = JsonReaderWriterFactory.CreateJsonWriter(stream, Encoding.UTF8))
        {
            serializer.WriteObject(writer, entity);
        }

        return Encoding.UTF8.GetString(stream.ToArray());
    }
}

Then you will only need to work a little bit with C# to populate the required DataSet with your object instance.

Here is my code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM