【问题标题】:read JSON Response in C# Xamarin.Forms在 C# Xamarin.Forms 中读取 JSON 响应
【发布时间】:2016-12-17 07:40:25
【问题描述】:

我正在我的 xamarin.forms android 应用程序中读取来自 web 服务的响应, 以下是包含状态(0-错误,1-OK)消息和信息的响应(信息包含数据表中的数据行)

{
"status": 1,
"msg" : "OK",
"info": {
"UCode": "1",
"UName": "Admin",
"UPass": "pass"
}
}

我可以阅读statusmsg

如何将节点 info 中的数据转换为 User_Info 类的 Observable Collection?

这是我的代码

            try
            {
                using (var client = new HttpClient())
                {
                    var url = GSVar.hostname + GSVar.user_check;
                    var content = new FormUrlEncodedContent(new[]
                    {
                        new KeyValuePair<string,string>("uname",T1.Text),
                        new KeyValuePair<string, string>("upass",T2.Text)
                    });

                    var resp = await client.PostAsync(new Uri(url), content);
                    //var resp = await client.GetAsync(new Uri(url));
                    if (resp.IsSuccessStatusCode)
                    {
                        var result = JsonConvert.DeserializeObject<Json_Respnce>(resp.Content.ReadAsStringAsync().Result);
                        if (result.status == 0)
                            General.GSErr(result.msg);
                        else
                        {
                            //User_Info user_info = JsonConvert.DeserializeObject<User_Info>(result.UserInfo);
                            //await DisplayAlert("OK", result.UserInfo.ToString(), "OK");
                        }
                    }
                    else
                        General.GSErr("Nothing retrieved from server.");
                }
            }
            catch { throw; }

列出类

class Json_Respnce
{
    [JsonProperty(PropertyName ="status")]
    public int status { get; set; }

    [JsonProperty(PropertyName = "msg")]
    public string msg { get; set; }

    //[JsonProperty(PropertyName = "info")]
    //public string UserInfo { get; set; }
}

class User_Info
{
    [JsonProperty(PropertyName = "UCode")]
    public string UCode { get; set; }

    [JsonProperty(PropertyName = "UName")]
    public string UName { get; set; }

    [JsonProperty(PropertyName = "UPass")]
    public string UPass { get; set; }
}

【问题讨论】:

  • 您确定需要将info 强制转换为ObservableCollection 吗?因为 json 使用的对象不是数组,通常会转换为列表类型。

标签: c# json xamarin


【解决方案1】:

创建所需的模型类。您可以使用json2csharp。只需将您的 JSON 字符串粘贴到那里,然后单击生成

public class Info
{
    public string UCode { get; set; }
    public string UName { get; set; }
    public string UPass { get; set; }
}

public class Response
{
    public int status { get; set; }
    public string msg { get; set; }
    public Info info { get; set; }
}

然后您可以将您的 JSON 字符串反序列化为:

string jsonString = await resp.Content.ReadAsStringAsync ();
Response response = JsonConvert.DeserializeObject<Response> (jsonString));

if (response.status == 1) {
    Info info = response.info
}

【讨论】:

  • 非常感谢。你拯救了我的一天
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-11
  • 1970-01-01
  • 2014-09-09
  • 2013-05-05
  • 1970-01-01
相关资源
最近更新 更多