程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#基礎知識 >> C#json 解析

C#json 解析

編輯:C#基礎知識

 

  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();
    }

 

  以上親測可用

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved