JSON

C#ÐòÁл¯ºÍ·´ÐòÁл¯×ۺϰ¸Àý(3)

×ÖºÅ+ ×÷ÕߣºH5Ö®¼Ò À´Ô´£ºH5Ö®¼Ò 2016-05-08 14:06 ÎÒÒªÆÀÂÛ( )

1.Ê×ÏÈ´´½¨Ò»¸öJsonToDataTableµÄͨÓ÷½·¨ Json ×Ö·û´® ת»»Îª DataTableÊý¾Ý¼¯ºÏ public DataTable JsonToDataTable ( string json){DataTable dataTable = new DataTable(); //ʵÀý»¯ DataTable result; try {Jav

1.Ê×ÏÈ´´½¨Ò»¸öJsonToDataTableµÄͨÓ÷½·¨

Json ×Ö·û´® ת»»Îª DataTableÊý¾Ý¼¯ºÏ public DataTable JsonToDataTable(string json) { DataTable dataTable = new DataTable(); //ʵÀý»¯ DataTable result; try { JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer(); javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //È¡µÃ×î´óÊýÖµ ArrayList arrayList = javaScriptSerializer.Deserialize<ArrayList>(json); if (arrayList.Count > 0) { foreach (Dictionary<string, object> dictionary in arrayList) { if (dictionary.Keys.Count == 0) { result = dataTable; return result; } if (dataTable.Columns.Count == 0) { foreach (string current in dictionary.Keys) { dataTable.Columns.Add(current, dictionary[current].GetType()); } } DataRow dataRow = dataTable.NewRow(); foreach (string current in dictionary.Keys) { dataRow[current] = dictionary[current]; } dataTable.Rows.Add(dataRow); //Ñ­»·Ìí¼ÓÐе½DataTableÖÐ } } } catch { } result = dataTable; return result; }

2.н¨Ò»¸öjson×Ö·û´®²¢¸³Öµ¡£È»ºóµ÷ÓÃÉÏÃæµÄ·½·¨¾Í¿ÉÒÔתΪDataTableÁË¡£

string json = string.Empty; json = "name\":\"Jason\",\"id\":20130001,\"phone\":\"13579246810\"}," + "," + ""; DataTable dt = JsonToDataTable(json); 3.ÈçºÎ°ÑDataTableתΪJSON string

1.н¨Ò»¸öDataTableToJsonµÄ·½·¨

Converts datatable to JSON string. (DataTable dt) { JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer(); javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //È¡µÃ×î´óÊýÖµ ArrayList arrayList = new ArrayList(); foreach (DataRow dataRow in dt.Rows) { Dictionary<string, object> dictionary = new Dictionary<string, object>(); //ʵÀý»¯Ò»¸ö²ÎÊý¼¯ºÏ foreach (DataColumn dataColumn in dt.Columns) { dictionary.Add(dataColumn.ColumnName, dataRow[dataColumn.ColumnName].ToString()); } arrayList.Add(dictionary); //ArrayList¼¯ºÏÖÐÌí¼Ó¼üÖµ } return javaScriptSerializer.Serialize(arrayList); //·µ»ØÒ»¸öjson×Ö·û´® }

2.µ÷Ó÷½·¨£¬´«ÈëÒ»¸öDataTable¾Í¿ÉÒÔ¡£ÄĸöDataTable¶¼¿ÉÒԵġ£

string json = DataTableToJson(dt); 4.ÈçºÎÐòÁл¯£¿

1.´´½¨Ò»¸ö·½·¨

ConvertS an object to a JSON string When the string length is long (Object o) { JavaScriptSerializer j = new JavaScriptSerializer(); j.MaxJsonLength = Int32.MaxValue; return j.Serialize(o); }

2.¾ÍÕâÑù´«ÈëÒ»¸ö¶ÔÏó¾ÍÐÐÁË¡£
±ÈÈ磺Person p = new Person();string json = ToJson2(p);
ÉÏÃæÔÚDataTableÖÐÒ²Óõ½¹ýÐòÁл¯µÄDemo£¬¿ÉÒԲο¼Óõ½´óÁ¿Êý¾Ýʱ½øÐÐתJSON

5.ÈçºÎ´ÓtxtÎļþÖжÁÈ¡JSON¸ñʽµÄÊý¾Ý²¢·´ÐòÁл¯

¼òÊöÏÂÁ÷³Ì£º
1.ÅжÏtxtµÄ´æÔÚ
2.¶ÁÈ¡txtµÄÄÚÈÝ
3.ͨ¹ýÏÂÃæµÄ´úÂë°ÑJSON¶ÔÏó´æÈëÒ»¸ö·ºÐÍÖÐ

List<Person> people = jss.Deserialize<List<Person>>(json);

4.ͨ¹ýÓ³É䣬×Ô¶¯»ñÈ¡µ½JSON¶ÔÓ¦Àà(Class)µÄÊôÐÔÃû£¬ÊµÏÖ×Ô¶¯»¯²¢Í¨¹ý±éÀúÊä³ö

PropertyInfo[] piArr = typeof(Person).GetProperties(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); if (people.Count == 0) { DeserializeToObject(json); return; } //Get the name and value of the specified class Person automatically. foreach (var person in people) { sb.Clear(); foreach (PropertyInfo pi in piArr) { sb.Append(pi.Name + "=" + pi.GetValue(person)); sb.Append("\t "); } listAll.Items.Add(sb.ToString()); //listAll.Items.Add("name=" + person.Name + "\tid=" + person.Id + "\tphone=" + person.Phone); }

¡¡

1.±¾Õ¾×ñÑ­ÐÐÒµ¹æ·¶£¬ÈκÎתÔصĸå¼þ¶¼»áÃ÷È·±ê×¢×÷ÕߺÍÀ´Ô´£»2.±¾Õ¾µÄÔ­´´ÎÄÕ£¬ÇëתÔØʱÎñ±Ø×¢Ã÷ÎÄÕÂ×÷ÕߺÍÀ´Ô´£¬²»×ðÖØÔ­´´µÄÐÐΪÎÒÃǽ«×·¾¿ÔðÈΣ»3.×÷ÕßͶ¸å¿ÉÄܻᾭÎÒÃDZ༭Ð޸Ļò²¹³ä¡£

Ïà¹ØÎÄÕÂ
  • C++ Builder ÀïÃæʹÓà JsonCpp ºÍ JsonCpp Ö§³Ö UNICODE (UTF

    C++ Builder ÀïÃæʹÓà JsonCpp ºÍ JsonCpp Ö§³Ö UNICODE (UTF

    2016-04-08 15:00

  • Ïê½âC++µÄJSON¾²Ì¬Á´½Ó¿âJsonCppµÄʹÓ÷½·¨

    Ïê½âC++µÄJSON¾²Ì¬Á´½Ó¿âJsonCppµÄʹÓ÷½·¨

    2016-04-02 14:00

  • C++µÄJson½âÎö¿â£ºjsoncpp

    C++µÄJson½âÎö¿â£ºjsoncpp

    2016-01-26 09:22

  • jsoncpp ¿âµÄc++ʵÏÖ,ÀïÃ滹ÓвâÊÔ³ÌÐò,ÊDz»¿É¶àµÃµÄѧϰ×ÊÁÏ Li

    jsoncpp ¿âµÄc++ʵÏÖ,ÀïÃ滹ÓвâÊÔ³ÌÐò,ÊDz»¿É¶àµÃµÄѧϰ×ÊÁÏ Li

    2016-01-14 19:29

ÍøÓѵãÆÀ
õ