Xamarin REST Web 服务不反序列化
Posted
技术标签:
【中文标题】Xamarin REST Web 服务不反序列化【英文标题】:Xamarin rest web service doesn't deserialize 【发布时间】:2016-11-23 07:02:06 【问题描述】:我想通过 get 方法使用 rest。我的代码如下;
public class RegisterPage : ContentPage
Label label, l4, label2;
public RegisterPage()
Button btn = new Button
Text = "register"
;
btn.Clicked += Btn_Clicked;
label = new Label();
l4 = new Label();
label2 = new Label();
Content = new StackLayout
Children =
btn,
label,
l4,
label2
;
private async void Btn_Clicked(object sender, EventArgs e)
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add(Constants.API_KEY_HEADER_KEY, Constants.API_KEY);
string URL = Constants.URL;
var response = await client.GetAsync(URL);
var content = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<Models.Result>(content);
label.Text = result.Success.ToString();
l4.Text = result.Error.ToString();
label2.Text = ((RegisteredDevice)result.Retval).Clientuuid + " - " + ((RegisteredDevice)result.Retval).Deviceuuid;
网址运行良好。我的内容值有 json 字符串。但是序列化不起作用。
var 结果 = JsonConvert.DeserializeObject(content); 此代码不会反序列化。
我的模型是;
public class Result
private object retval = null;
private bool success = false;
private Error error = null;
internal Error Error
get return error;
set error = value;
public bool Success
get return success;
set success = value;
public object Retval
get return retval;
set retval = value;
json:
"result":
"retail":
"@xsi.type":"registeredDevice",
"clientuuid":"28asgargb-acfe-41dfgsdg51",
"deviceuuid":123456
,
"success":true
【问题讨论】:
您的 JSON 内容是什么样的? "result":"retval":"@xsi.type":"registeredDevice","clientuuid":"28asgargb-acfe-41dfgsdg51","deviceuuid":123456,"成功":true @SushiHangover 【参考方案1】:我认为问题出在:
private object retval = null;
所以对我来说,在 C# 中构造序列化对象的最佳方法是使用这个网站:
http://json2csharp.com/
这会告诉你你的json是否正确,他会为你生成你需要的类,这里是json2csharp生成的类
public class Retail
public string __invalid_name__@xsi.type get; set;
public string clientuuid get; set;
public int deviceuuid get; set;
public class Result
public Retail retail get; set;
public bool success get; set;
public class RootObject
public Result result get; set;
【讨论】:
非常感谢,它运行良好。再次感谢您以上是关于Xamarin REST Web 服务不反序列化的主要内容,如果未能解决你的问题,请参考以下文章