C#json 解析

來源:互聯網
上載者:User

標籤:

 

  Json範例一:

string jsonText=

{Code":0,"Message":"成功","Data":{"BoardProvinceName":"上海","BoardCityName":"上海","BoardDetailAddress":"普陀區怒江北路449弄8號C5","CourierName":"15601804660","CourierMobile":"15601804660","CargoNo":89,"BoardNo":3,"BoxNo":2,"DoorNo":3,"ExpressNo":"333333","CargoPassword":null,"Size":3,"Status":2,"StoreTime":"\/Date(1406772770183)\/","ExtractionTime":"\/Date(1415185496877)\/","CustomerMobile":"15601804660","CourierPId":"321323198902082117"}}}

 

JObject jo = JObject.Parse(jsonText);string strCode= jo["Code"].ToString();//Code:0
string ddaf = GetJsonValue(jo.Children(), "BoardDetailAddress");//BoardDetailAddress:普陀區怒江北路449弄8號C5

 

   =====================================================================

 

  不帶==》 “[”
 string jsonData = "{\"name\":\"lily\",\"age\":23,\"addr\":{\"city\":\"guangzhou\",\"province\":\"guangdong\"}}";            JObject jsonObj = JObject.Parse(jsonData);                      string strname = jsonObj["name"].ToString();            string strprovince = GetJsonValue(jsonObj.Children(), "province");

 

   =====================================================================

 

 類:

 public  static string GetJsonValue(JEnumerable<JToken> jToken, string key)        {            IEnumerator enumerator = jToken.GetEnumerator();            while (enumerator.MoveNext())            {                JToken jc = (JToken)enumerator.Current;                if (jc is JObject || ((JProperty)jc).Value is JObject)                {                    return GetJsonValue(jc.Children(), key);                }                else                {                    if (((JProperty)jc).Name == key)                    {                        return ((JProperty)jc).Value.ToString();                    }                }            }            return null;        }

 =================================================================================

        

  帶==》 “[”
    string jsonText ="{‘name‘:‘test‘,‘phone‘:‘18888888888‘,‘image‘:[{‘name‘:‘img1‘,‘data‘:‘data1‘},{‘name‘:‘img2‘,‘data‘:‘data2‘},{‘name‘:‘img3‘,‘data‘:‘data3‘}]}";    string jsonText1 = "{‘name‘:‘test‘,‘phone‘:‘18888888888‘,‘image‘:[{‘name‘:‘img1‘,‘data‘:‘data1‘}]}";     JObject jo = (JObject)JsonConvert.DeserializeObject(jsonText);     string zone = jo["name"].ToString();     string zone_en = jo["phone"].ToString();     JArray jar = JArray.Parse(jo["image"].ToString());    string c = zone;    string d = zone_en;    for (var i = 0; i < jar.Count; i++)   {       JObject j = JObject.Parse(jar[i].ToString());       string a = j["name"].ToString();      string b = j["data"].ToString();    }

  

   多層嵌套

string jsonData1 = "{\"addr\":[{\"city\":\"guangzhou\",\"province\":\"guangdong\"},{\"city\":\"guiyang\",\"province\":\"guizhou\"}]}";            JObject jsonObj1 = JObject.Parse(jsonData1);            JArray jar1 = JArray.Parse(jsonObj1["addr"].ToString());            JObject j0 = JObject.Parse(jar1[0].ToString());            JObject j1 = JObject.Parse(jar1[1].ToString());            string city1 = j0["city"].ToString();   //guangzhou            string city2 = j0["city"].ToString();   //guiyang

  

 

  以上親測可用

 

C#json 解析

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.