{ "programmers": [
{ "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" },
{ "firstName": "Jason", "lastName":"Hunter", "email": "bbbb" },
{ "firstName": "Elliotte", "lastName":"Harold", "email": "cccc" }
],
"authors": [
{ "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" },
{ "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" },
{ "firstName": "Frank", "lastName": "Peretti", "genre": "christian fiction" }
],
"musicians": [
{ "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" },
{ "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano" }
] }
最好能写注释 发给完整的版本最好
{ "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" },
{ "firstName": "Jason", "lastName":"Hunter", "email": "bbbb" },
{ "firstName": "Elliotte", "lastName":"Harold", "email": "cccc" }
],
"authors": [
{ "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" },
{ "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" },
{ "firstName": "Frank", "lastName": "Peretti", "genre": "christian fiction" }
],
"musicians": [
{ "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" },
{ "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano" }
] }
最好能写注释 发给完整的版本最好
MatchCollection matches=Regex.Matches(strInput,"\"(\w+)\"":\[\{\"(\w+)\":\"(\w+)\",\"(\w+)\":\"(\w+)\",\"(\w+)\":\"(\w+)\"\},\{\"(\w+)\":\"(\w+)\",\"(\w+)\":\"(\w+)\",\"(\w+)\":\"(\w+)\"\},\{\"(\w+)\":\"(\w+)\",\"(\w+)\":\"(\w+)\",\"(\w+)\":\"(\w+)\"\}\]);
//matches里面的数据就是了
/// {table:[{column1:1,column2:2,column3:3},{column1:1,column2:2,column3:3}]}
/// </summary>
/// <param name="strJson">Json字符串</param>
/// <returns></returns>
public static DataTable JsonToDataTable(string strJson)
{
//取出表名
var rg = new Regex(@"(?<={)[^:]+(?=:\[)", RegexOptions.IgnoreCase);
string strName = rg.Match(strJson).Value;
DataTable tb = null;
//去除表名
strJson = strJson.Substring(strJson.IndexOf("[") + 1);
strJson = strJson.Substring(0, strJson.IndexOf("]"));
//获取数据
rg = new Regex(@"(?<={)[^}]+(?=})");
MatchCollection mc = rg.Matches(strJson);
for (int i = 0; i < mc.Count; i++)
{
string strRow = mc[i].Value;
string[] strRows = strRow.Split(',');
//创建表
if (tb == null)
{
tb = new DataTable();
tb.TableName = strName;
foreach (string str in strRows)
{
var dc = new DataColumn();
string[] strCell = str.Split(':');
dc.ColumnName = strCell[0];
tb.Columns.Add(dc);
}
tb.AcceptChanges();
}
//增加内容
DataRow dr = tb.NewRow();
for (int r = 0; r < strRows.Length; r++)
{
dr[r] = strRows[r].Split(':')[1].Trim().Replace(",", ",").Replace(":", ":").Replace("\"", "");
}
tb.Rows.Add(dr);
tb.AcceptChanges();
}
return tb;
}
引用
Newtonsoft.Json.Linq
string p = HttpContext.Current.Server.MapPath(@"~/daxue.txt");
var txt1 = File.ReadAllLines(p, Encoding.GetEncoding("GB2312"));//读取txt
string con = txt1.FirstOrDefault();
JavaScriptSerializer jss = new JavaScriptSerializer();
List<country> langs = jss.Deserialize<List<country>>(con);
foreach (country m in langs)
{
try
{
object[] shengs = (object[])m.provs;
foreach (object sheng in shengs)
{
Dictionary<string, object> sh = (Dictionary<string, object>)sheng;
//解析大学
object[] daxues = (object[])sh["univs"];
foreach (object daxue in daxues)
{
Dictionary<string, object> dx = (Dictionary<string, object>)daxue;
Response.Write(m.name + "$" + sh["name"].ToString() + "$" + dx["name"].ToString() + "<br/>");
}
}
}
catch (Exception ex)
{
object[] daxues = (object[])m.univs;
foreach (object daxue in daxues)
{
Dictionary<string, object> dx = (Dictionary<string, object>)daxue;
Response.Write(m.name + "$-$" + dx["name"].ToString() + "<br/>");
}
//Response.Write(ex.Message + "<br/>");
} }
} class country
{
public string id { get; set; }
public string name { get; set; }
public object provs { get; set; }
public object univs { get; set; } }
这是我前几天,解析人人网大学校名数据的代码,你可以参考下,
大体思路是,按照数据格式写一个类,然后反序列化JSON