How are we going to parse the JSON grid? In. NET, we are the json.net, yes, we can still use him in Xamarin
Thanks to the community for its great contribution download URL: http://components.xamarin.com/view/json.net/
Continue on the previous project we introduced the download Newtonsoft.Json.dll
Remember to introduce Android under the
As we normally use json.net, we'll start by creating a corresponding class for the data that we've taken back from Facebook.
At this time we can use http://json2csharp.com/this website to help
Create a user class in the project
View Sourceprint?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.
}
We go back to the main program
View Sourceprint?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.
}
Results:
is not very simple, under Xamarin development Android and the past experience is the same
Xamarin calls Json.NET to parse JSON