C#解析Json键值对


1. 使用Newtonsoft.Json.dll。

      引用:using Newtonsoft.Json.Linq;

 1 string data_json = "{\"codetype\": \"1\", \"pagram\": { \"return_url \": \"http: //www.alipay.com\", \"scene\": \"bar_code\", \"buyer_id\": \"2088101568338364\", \"auth_code\": \"28763443825664394 \",
\"extend_params\": \"foreigner \", \"card_org_no\": \"00011 \" } }
"; 2 var jsObject = JObject.Parse(data_json); 3 4 foreach (JToken child in jsObject.Children()) 5 { 6 var property1 = child as JProperty; 7 MessageBox.Show(property1.Name + ":" + property1.Value); 8 } 9 10 11 多层Json 12 13 14 var jsObject = JObject.Parse(data_json); 15 16 foreach (JToken child in jsObject.Children()) 17 { 18 //var property1 = child as JProperty; 19 //MessageBox.Show(property1.Name + ":" + property1.Value); 20 foreach (JToken grandChild in child) 21 { 22 foreach (JToken grandGrandChild in grandChild) 23 { 24 var property = grandGrandChild as JProperty; 25 if (property != null) 26 { 27 MessageBox.Show(property.Name + ":" + property.Value); 28 } 29 } 30 } 31 } 32


2.

string strjson = @"{
    "codetype": "1",
    "pagram": {
        "return_url ": "http: //www.alipay.com",
        "scene": "bar_code",
        "buyer_id": "2088101568338364",
        "auth_code": "28763443825664394 ",
        "extend_params": "foreigner ",
        "card_org_no": "00011 "
    }
}";

   var json = Newtonsoft.Json.JsonConvert.DeserializeObject(pagram) as JObject;
   string data = json["data"].ToString();


Console.WriteLine(
"{0} {1} {2}", json["data"].ToString());

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM