Intermediary transaction SEO diagnosis Taobao guest Cloud host technology Hall
In the next two years data storage has swept the world in JSON format. Each large and small Web site uses the JSON format to store information about details or read-only (non-query filter criteria). When the C # background code reads the JSON format into a DataTable or other object, the JSON string data is extremely cumbersome to extract certain values.
Now let's look at using the most primitive method (array splitting method) to extract some values from JSON string data:
Example 1: Suppose we have a data store that is Web site information:
String txtrent = @ "{' Site name ': ' Brain ball ', ' website address ': ' http://www.naoqiu.com ', ' IP ': ' 192.168.0.1 ', ' Integrated rank ': ' No ranking Data '},
{' Site name ': ' Home menu ', ' website address ': ' http://shipu.naoqiu.com ', ' IP ': ' 192.168.0.2 ', ' Integrated ranking ': ' 12345678 '},
{' website name ': ' Bus Enquiry net ', ' website address ': ' http://bus.naoqiu.com ', ' IP ': ' 192.168.1.2 ', ' Integrated ranking ': ' 12345678 '} ';
Next we need to get the website address from this information, the number of components to disassemble the code as follows:
<summary>
Array Splitting method
</summary>
<returns></returns>
public static string] Getrentinfo ()
{
String txtrent = @ "{' Site name ': ' Brain ball ', ' website address ': ' http://www.naoqiu.com ', ' IP ': ' 192.168.0.1 ', ' Integrated rank ': ' No ranking Data '},
{' Site name ': ' Home menu ', ' website address ': ' http://shipu.naoqiu.com ', ' IP ': ' 192.168.0.2 ', ' Integrated ranking ': ' 12345678 '},
{' website name ': ' Bus Enquiry net ', ' website address ': ' http://bus.naoqiu.com ', ' IP ': ' 192.168.1.2 ', ' Integrated ranking ': ' 12345678 '} ';
String] items = Txtrent.trimend ('} '). Split ('} ');
String] newitems=new String[items. Length];
int i = 0, index = 0;
string tem;
foreach (string s in items) {
index = S.indexof ("website address");
TEM = s.substring (index + 7);
newitems[i++] = tem. Split (' \ ') [0];
}
return newitems;
}
Although this method can implement the function, but it seems that the code is written in a bit of complexity, execution efficiency, then how do we improve the efficiency of the code, and simplicity? The answer is yes, use regular to get the appropriate data:
<summary>
regular base Application
</summary>
<returns></returns>
public static string] Getinfo_domain () {
String txtrent = @ "{' Site name ': ' Brain ball ', ' website address ': ' http://www.naoqiu.com ', ' IP ': ' 192.168.0.1 ', ' Integrated rank ': ' No ranking Data '},
{' Site name ': ' Home menu ', ' website address ': ' http://shipu.naoqiu.com ', ' IP ': ' 192.168.0.2 ', ' Integrated ranking ': ' 12345678 '},
{' website name ': ' Bus Enquiry net ', ' website address ': ' http://bus.naoqiu.com ', ' IP ': ' 192.168.1.2 ', ' Integrated ranking ': ' 12345678 '} ';
MatchCollection matches = regex.matches (txtrent, @ "<=") [\:\ ' ^\ '];
String] newitems=new string[matches. Count];
int i = 0;
foreach (Match m in matches) {
newitems[i++] = M.value;
}
return newitems;
}
Summary: See if this method feels is very convenient! Regular applications are very extensive, such as the extraction of web content. The next section explains the use of regular customizations to make JSON and DataTable go with each other.
Resources on the Internet and references in this paper
Microsoft's regular expression tutorial
System.Text.RegularExpressions.Regex Class (MSDN)
Professional Regular Expression Teaching website (English)
On. A detailed discussion of the balancing group under net
Mastering Regular Expressions (Second Edition)
A regular expression 30-minute introductory tutorial
And, of course, Microsoft. Implement JSON API in net
If you want to use the net JSON API you can look at this article: http://www.cnblogs.com/litongtong/archive/2008/01/12/1036312.html