【发布时间】:2016-06-04 12:35:30
【问题描述】:
我正在尝试在 Xamarin Forms (C#) 中使用我的 JSON API。 API 可以返回:
失败时:{"status":"failed"}
一切正常时:{"status":"success","time":"03.06.2016 13:15","data":["Thats a cool text","And thats another text"]}
我目前正在这样做:
var client = new HttpClient ();
var url = "http://domain/plan/web/get/" + userclass;
var response = await client.GetAsync (url);
var data = response.Content.ReadAsStringAsync ().Result;
但是现在,我需要:
- 将 JSON 数据提取到数组或其他东西中
- API 返回失败时显示警报(使用 DisplayAlert())
- 当 API 返回成功时,获取时间,并将数据(API 结果末尾的两个或多个字符串)设置为标签或列表视图。
我该怎么做?
【问题讨论】:
-
创建一个处理 JSON 数据的类。使用 Newtonsoft.json Nuget 包将 json 转换为对象。
-
但是像两个字符串这样的数据怎么获取,因为它就像JSON中的一个数组。
-
创建一个模型来处理你的数据,然后你使用
JsonConvert.Serialize(yourobject)将它变成一个字符串,当你获取它时你反序列化你的字符串来取回你的对象JsonConvert.Deserialize<yourobject>(jsonstring);。
标签: c# json xamarin xamarin.forms fetch