Xamarin 调用JSON.net来解析JSON 转(Model) json2csharp.com/
2024-08-21 18:48:02
- 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 {
get
;
set
; }
06.
public
string
name {
get
;
set
; }
07.
public
string
first_name {
get
;
set
; }
08.
public
string
last_name {
get
;
set
; }
09.
public
string
link {
get
;
set
; }
10.
public
string
username {
get
;
set
; }
11.
public
string
gender {
get
;
set
; }
12.
public
string
locale {
get
;
set
; }
13.
}
14.
}
我们回到主程序
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();
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 跟过去的体验是相同的
-
最新文章
- Android下的屏幕适配
- word常用操作
- uglifyjs压缩JS的
- Swift定义单例
- 《ASP.NET MVC4 WEB编程》学习笔记------ViewBag、ViewData和TempData的使用和区别
- Android 开发-Intent传递普通字符串
- codeforces 192a
- vs2005_the breakpoint will not currently be hit. The source code is different from the original verison.
- MultiWii MWC的软件和调试方法
- View not attached to window manager crash 的解决办法
- html+js+ashx+easyui+ado.net权限管理系统
- Android 5.x 权限问题解决方法
- \r,\n,\r\n的区别和用法
- nohup ./startWebLogic.sh >;out.log 2>;&;1 &; 解析
- [BZOJ2457][BeiJing2011]双端队列 (单调性)
- 调用Android自带浏览器打开网页
- Linux git 在自己的服务器上建立 git 仓库(repository)
- [UE4]Spacer
- 剖析QMenu &; Qt完全定制化菜单
- R实战 第十篇:列联表和频数表