Json與數組

來源:互聯網
上載者:User

標籤:des   class   code   java   c   ext   

今天趁著看原始碼的同時,記錄學習的小知識。

一、String.Split 方法有6個重載函數:

1) public string[] Split(params char[] separator)
2) public string[] Split(char[] separator, int count)
3) public string[] Split(char[] separator, StringSplitOptions options)
4) public string[] Split(string[] separator, StringSplitOptions options)
5) public string[] Split(char[] separator, int count, StringSplitOptions options)
6) public string[] Split(string[] separator, int count, StringSplitOptions options)


下邊我們通過一些執行個體來說明下怎麼使用(以下string words = "1,2.3,,4";):

1. public string[] Split(params char[] separator)

傳回值:

類型:System.String[]
separator.‘ data-guid="7b28a9466989f487697b9306b7388e52">一個數組,其元素包含此執行個體中的子字串,這些子字串由 separator 中的一個或多個字元分隔。 有關更多資訊,請參見“備忘”一節。 

string[] split = words.Split(new Char[] { ‘,‘ });//返回:{"1","2.3","","4"}
string[] split = words.Split(new Char[] { ‘,‘, ‘.‘ });//返回:{"1","2","3","","4"}


2. public string[] Split(char[] separator, int count)

string[] split = words.Split(new Char[] { ‘,‘, ‘.‘ }, 2);//返回:{"1","2.3,,4"}
string[] split = words.Split(new Char[] { ‘,‘, ‘.‘ }, 6);//返回:{"1","2","3","","4"}


3. public string[] Split(char[] separator, StringSplitOptions options)

string[] split = words.Split(new Char[] { ‘,‘, ‘.‘ }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
string[] split = words.Split(new Char[] { ‘,‘, ‘.‘ }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素


4. public string[] Split(string[] separator, StringSplitOptions options)

string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素


5. public string[] Split(char[] separator, int count, StringSplitOptions options)

string[] split = words.Split(new Char[] { ‘,‘, ‘.‘ }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
string[] split = words.Split(new Char[] { ‘,‘, ‘.‘ }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素


6. public string[] Split(string[] separator, int count, StringSplitOptions options)

string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素


需要注意的是沒有重載函數public string[] Split(string[] separator),所以我們不能像VB.NET那樣使用words.Split(","),而只能使用words.Split(‘,‘)!很多人都很奇怪為什麼把雙引號改為單引號就可以了?看了上邊的重載函數該知道答案了吧^_^

 

二、JSON.stringify 函數

將 JavaScript 轉換為 JavaScript 物件標記法 (JSON) 字串。

JSON.stringify(value [, replacer] [, space])

Converts a JavaScript value to a JavaScript Object Notation (JSON) string.

Parameters:

value

Required. A JavaScript value, usually an object or array, to be converted.

replacer

Optional. A function or array that transforms the results.

If replacer is a function, JSON.stringify calls the function, passing in the key and value of each member. The return value is used instead of the original value. If the function returns undefined, the member is excluded. The key for the root object is an empty string: "".

If replacer is an array, only members with key values in the array will be converted. The order in which the members are converted is the same as the order of the keys in the array. The replacer array is ignored when the value argument is also an array.

space

Optional. Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.

If space is omitted, the return-value text is generated without any extra white space.

If space is a number, the return-value text is indented with the specified number of white spaces at each level. If space is greater than 10, text is indented 10 spaces.

If space is a non-empty string, such as ‘\t‘, the return-value text is indented with the characters in the string at each level.

If space is a string that is longer than 10 characters, the first 10 characters are use。 

Example:

var contact = new Object();contact.firstname = "Jesper";contact.surname = "Aaberg";contact.phone = ["555-0100", "555-0120"];var memberfilter = new Array();memberfilter[0] = "surname";memberfilter[1] = "phone";var jsonText = JSON.stringify(contact, memberfilter, "\t");document.write(jsonText);// Output: // { "surname": "Aaberg", "phone": [ "555-0100", "555-0120" ] }

 

 三、DeserializeObject 方法

public Object DeserializeObject(string input) 
參數
input
類型:System.String
要進行還原序列化的 JSON 字串。
傳回值類型:System.Object
還原序列化的對象。

 
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.