Address: http://www.php100.com/html/webkaifa/javascript/2013/0125/11993.html
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 used on various networks, platforms, and Program . JSON syntax is very simple ,...
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: JSON rules are 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. Separate the parallel data with commas. Name 1: value 1, name 2: Value 2
3. The ing set (object) is represented by braces. { Name 1: value 1, Name 2: Value 2 }
4. The set (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. Use a colon (rather than an equal sign) to assign values to JSON. 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 structure composed of key-value pairs. This data structure has different implementations in different languages. For example, JavaScript is an object, while Java is a map structure,C LanguageIt is struct, and other languages can be record and hash table. (2) Ordered Sets. This data structure can be implemented by list, vertor, array, and sequence in different languages.
7. Processing of JSON data in C #
List <t> to JSON [CSHARP]
Public Static String Obj2json <t> (T data ){ Try {System. runtime. serialization. JSON. datacontractjsonserializer serializer = New System. runtime. serialization. JSON. datacontractjsonserializer (data. GetType ()); Using (Memorystream MS = New Memory stream () {serializer. writeobject (MS, data ); Return Encoding. utf8.getstring (Ms. toarray ());}} Catch { Return Null ;}}
JSON to list <t>
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
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 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
Public StaticT json2obj <t> (StringJSON) {T OBJ= Activator. createinstance <t>();Using(System. Io. memorystream MS =NewSystem. Io. memorystream (system. Text. encoding. utf8.getbytes (JSON) {system. runtime. serialization. JSON. datacontractjsonserializer serializer=NewSystem. runtime. serialization. JSON. datacontractjsonserializer (obj. GetType ());Return(T) serializer. readobject (MS );}}