Get the App
SLTechnology News&Howtos  ›  Development  › 

How to convert DataTable and Dataset to Json data in C #

Shulou Source: shulou.com Published: 2022-05-31 20:01:05 09月22日 Update

Today, the editor will share with you the relevant knowledge points about how to convert DataTable and Dataset to Json data in C#. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.

What is JSON?

JSON is the abbreviation of JavaScript Object Notation, which means "JavaScript object representation" in Chinese. It is a text format for data exchange, not a programming language.

JSON is a lightweight data exchange format based on a subset of ECMAScript (the js specification developed by W3C) and uses a text format that is completely independent of the programming language to store and represent data. The concise and clear hierarchical structure makes JSON an ideal data exchange language. It is easy for people to read and write, at the same time, it is also easy for machine analysis and generation, and effectively improve the efficiency of network transmission.

Characteristics of JSON

JSON mainly has the following features, which make it an ideal data exchange language:

JSON is a lightweight text data interchange format

JSON is self-descriptive and easier to understand

JSON uses a completely language-independent text format: JSON uses JavaScript syntax to describe data objects, but JSON is still language-and platform-independent. JSON parsers and JSON libraries support many different programming languages. At present, common dynamic programming languages (PHP,JSP,.NET) support JSON.

JSON is a syntax for storing and exchanging text information. It has the same characteristics as XML. It is a data storage format, but it is smaller, faster, easier for people to write and read, easier to generate and parse than XML.

Features similar to XML:

JSON is plain text

JSON is "self-descriptive" (human readable)

JSON has a hierarchical structure (values exist in values)

JSON can be parsed through JavaScript

JSON data can be transmitted using AJAX

The differences compared to XML:

There is no closing label

Shorter

Read and write faster

Can be parsed using the built-in JavaScript eval () method

Use array

Do not use reserved words

DataTable to Json public static string DataTableToJson1 (string jsonName, DataTable dt) {StringBuilder Json = new StringBuilder (); Json.Append ("{\"+ jsonName +"\ ": ["); if (dt.Rows.Count > 0) {for (int I = 0; I)

< dt.Rows.Count; i++) { Json.Append("{"); for (int j = 0; j < dt.Columns.Count; j++) { Json.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":\"" + dt.Rows[i][j].ToString() + "\""); if (j < dt.Columns.Count - 1) { Json.Append(","); } } Json.Append("}"); if (i < dt.Rows.Count - 1) { Json.Append(","); } } } Json.Append("]}"); return Json.ToString(); }Dataset转Json主方法 public static string Dataset2Json(DataSet ds) { StringBuilder json = new StringBuilder(); foreach (DataTable dt in ds.Tables) { json.Append("{\""); json.Append(dt.TableName); json.Append("\":"); json.Append(DataTable2Json(dt)); json.Append("}"); } return json.ToString(); }调用方法 public static string DataTableToJson2(DataTable dt) { StringBuilder Json = new StringBuilder(); Json.Append("{\"" + dt.TableName + "\":["); if (dt.Rows.Count >

0) {for (int I = 0; I < dt.Rows.Count; iTunes +) {Json.Append ("\ r\ n" + "{\" device\ ":" + "\ r\ n" + "{"); for (int j = 0; j < dt.Columns.Count String columValue +) {string columName = dt.Columns [j] .ColumnName.ToString (); string columValue = dt.Rows [I] [j] .ToString () If (j = = 9) / / "radios": {"wifiMAC": ~ ~}, Json.Append ("\" radios\ ": {\"+ columName +"\ ":\"+ columValue +"\ "}") Else if (j = = 10 | | j = = 11) / / "partDetails": {"mainLogicBoardSerialNumber": ~, "batterySerialNumber": ~} {if (j = = 10) Json.Append ("\" partDetails\ ": {\"+ columName +"\ ":\" + columValue + "\") Else Json.Append ("\"+ columName +"\ ":\"+ columValue +"\ "}") } else if (j = = 12) Json.Append ("\" order\ ": {\"+ columName +"\ ":\"+ columValue +"\ "") Else if (j = = 16) Json.Append ("\"+ columName +"\ ":\"+ columValue +"\ "}"); else Json.Append ("\"+ columName +"\ ":\"+ columValue +"\ "") If (j < dt.Columns.Count-1) Json.Append (",");} Json.Append ("}"); if (I < dt.Rows.Count-1) {Json.Append (",") } Json.Append ("\ r\ n" + "]}"); return Json.ToString ();} above is all the contents of the article "how to convert DataTable and Dataset to Json data in C#". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Tags: Data language text formatting features knowledge articles programming languages programming differences methods storage independence caching content objects descriptive faster ideal structure Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia MySQL Docker Shulou Tech Info MariaDB