Xamarin调用JSON.net来解析JSON

    •  
    • https://www.cnblogs.com/zjoch/p/4458516.html
    • \
       

      再来我们要怎么解析JSON格示呢?在.net 中,我们很孰悉的JSON.net,没错,我们依然可以在Xamarin中使用他

      感谢社群伟大的贡献 下载网址: http://components.xamarin.com/view/json.net/

       \
       

      接续上一个项目我们多引入下载后的 Newtonsoft.Json.dll

       

      \
       

      记得要引入Android 下的

       

      \
       

      引入后就跟我们平常使用JSON.net 一样首先我们要为Facebook接回来的数据建立一个相对应的Class

      这时候我们可以使用 http://json2csharp.com/ 这网站帮忙

       \
       

      在项目中建立一个User 的Class

       

       
      01. namespace SampleForWebClient
      02. {
      03. public class User
      04. {
      05. public string id { getset; }
      06. public string name { getset; }
      07. public string first_name { getset; }
      08. public string last_name { getset; }
      09. public string link { getset; }
      10. public string username { getset; }
      11. public string gender { getset; }
      12. public string locale { getset; }
      13. }
      14. }

      我们回到主程序

       

       
      01. using <a href="http://www.it165.net/pro/ydad/" target="_blank" class="keylink">Android</a>.App;
      02. using Android.Widget;
      03. using Android.OS;
      04. using Newtonsoft.Json;
      05.  
      06. namespace SampleForWebClient
      07. {
      08. [Activity(Label = "Json.net测试", MainLauncher = true, Icon = "@drawable/icon")]
      09. public class Activity1 : Activity
      10. {
      11. protected override void OnCreate(Bundle bundle)
      12. {
      13. base.OnCreate(bundle);
      14.  
      15. // Set our view from the "main" layout resource
      16. SetContentView(Resource.Layout.Main);
      17.  
      18. var btnGetData1 = FindViewById<Button>(Resource.Id.btnGetData1);
      19.  
      20. btnGetData1.Click += btnGetData1_Click;
      21. }
      22.  
      23. void btnGetData1_Click(object sender, System.EventArgs e)
      24. {
      25. var webClient = new System.Net.WebClient();
      26. var result = webClient.DownloadString("https://graph.facebook.com/donma.hsu");
      27. //透过JSON.net 反序列化为User对象
      28. var user = JsonConvert.DeserializeObject<User>(result);
      29. //印出 id and name
      30. Toast.MakeText(this, user.id+":"+user.name, ToastLength.Long).Show();
      31. }
      32.  
      33.  
      34. }
      35. }

      结果:
       

      \
       

      是不是很简单,在Xamarin 下面开发Android 跟过去的体验是相同的

转载于:https://www.cnblogs.com/LuoEast/p/8000497.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值