JSON Data Format

Source: Internet
Author: User
JSON is a lightweight data exchange format. It is a subset based on javascript syntax standards. JSON is a lightweight data exchange format. JSON is in a language-independent text format and can be easily transmitted between various networks, platforms, and programs. JSON syntax is very simple,... SyntaxHighlighter. all (); JSON is a lightweight data exchange format. It is a subset based on javascript syntax standards. JSON is a lightweight data exchange format. JSON is in a language-independent text format and can be easily transmitted between various networks, platforms, and programs. The JSON syntax is simple, easy for reading and writing, and easy for machine parsing and generation. To be familiar with json data, you must first understand json data: The JSON rule is simple: the object is an unordered set of "'name/value' pairs. An object starts with "{" (left parenthesis) and ends with "}" (right Parenthesis. Each "name" is followed by a ":" (colon); "," (comma) is used to separate the "name/value" pairs. The rules are as follows: 1. The ing is represented by a colon. Name: Value 2. separated by commas. Name 1: value 1, name 2: Value 23, the ing set (object) is represented by braces. {Name 1: value 1, name 2: Value 2} 4. the array of the parallel data is represented by square brackets. [{Name 1: value, name 2: Value 2}, {name 1: value, name 2: Value 2}] 5. Possible element values: string, number, object, array, true, false, null Note: 1. JSON is assigned with a colon (rather than an equal sign. Each value assignment statement is separated by a comma. The entire object is enclosed in braces. You can use braces to classify and nest data. 2. The data stored in the object description can be a string, number, or Boolean value. Object descriptions can also store functions, that is, object methods. 6. JSON mainly has two data structures (1) data structures composed of key-value pairs. This data structure has different implementations in different languages. for example, javascript is an object. in java, It is a Map structure, in C language it is struct, other languages can be record, hash table, and so on. (2) Ordered Sets. This data structure can be implemented by list, vertor, array, and sequence in different languages. 7. List of json data format processing in C # Convert Json [csharp] public static string Obj2Json (T data) {try {System. runtime. serialization. json. dataContractJsonSerializer serializer = new System. runtime. serialization. json. dataContractJsonSerializer (data. getType (); using (MemoryStream MS = new MemoryStream () {serializer. writeObject (MS, data); return Encoding. UTF8.GetString (ms. toArray () ;}} catch {return null ;}} convert Json to List [Csharp] public static Object Json2Obj (String json, Type t) {try {System. runtime. serialization. json. dataContractJsonSerializer serializer = new System. runtime. serialization. json. dataContractJsonSerializer (t); using (MemoryStream MS = new MemoryStream (Encoding. UTF8.GetBytes (json) {return serializer. readObject (MS) ;}} catch {return null ;}} DataTable to Json [csharp] public static string DataTable2Json (DataTable dt) {if (dt. rows. count = 0) {return "";} StringBuilder jsonBuilder = new StringBuilder (); // jsonBuilder. append ("{"); // jsonBuilder. append (dt. tableName. toString (); jsonBuilder. append ("["); // convert to multiple models in the form of for (int I = 0; I <dt. rows. count; I ++) {jsonBuilder. append ("{"); for (int j = 0; j <dt. columns. count; j ++) {jsonBuilder. append ("\" "); jsonBuilder. append (dt. columns [j]. columnName); jsonBuilder. append ("\": \ ""); jsonBuilder. append (dt. rows [I] [j]. toString (); jsonBuilder. append ("\", ");} jsonBuilder. remove (jsonBuilder. length-1, 1); jsonBuilder. append ("},");} jsonBuilder. remove (jsonBuilder. length-1, 1); jsonBuilder. append ("]"); // jsonBuilder. append ("}"); return jsonBuilder. toString ();} convert a single object to JSON [csharp] public static T Json2Obj (String json) {T obj = Activator. CreateInstance (); Using (System. IO. memoryStream MS = new System. IO. memoryStream (System. text. encoding. UTF8.GetBytes (json) {System. runtime. serialization. json. dataContractJsonSerializer serializer = new System. runtime. serialization. json. dataContractJsonSerializer (obj. getType (); return (T) serializer. readObject (MS );}}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.