适用于 Windows Phone 的 json 解析问题

本文关键字:问题 json Windows Phone 适用于 | 更新日期: 2023-09-27 18:35:37

  {
   "code": 0,
   "message": "success",
   "expense": {
           "account_name": "Printing and Stationery",
           "paid_through_account_name": "Petty Cash"
              }
  }

这是我的 json 格式,我尝试使用以下类对其进行反序列化

   [DataContract]
public class Expenses
{
    [DataMember(Name = "code")]
    public int Code { get; set; }
    [DataMember(Name = "message")]
    public string Message { get; set; }
    [DataMember(Name = "expense")]
    public Expense Expense { get; set; }
}
[DataContract]
public class Expense
{
    [DataMember(Name = "account_name")]
    public string Account_Name { get; set; }
    [DataMember(Name = "paid_through_account_name")]
    public int Paid_Through_Account_Name { get; set; }
}

我在下面的代码的帮助下调用了这个类

     var myObjects = JsonConvert.DeserializeObject<Expenses>(json);

但是在执行上述行时,我总是收到一个错误,指出:

     An exception of type 'System.UnauthorizedAccessException' occurred in     PhoneApp18.DLL but was not handled in user code
 If there is a handler for this exception, the program may be safely continued.

帮助我摆脱这个问题....

适用于 Windows Phone 的 json 解析问题

你有json"paid_through_account_name":"零用金",其中"零用金"是string。但是在您的模型属性中,公共Paid_Through_Account_Name具有类型 int 。尝试将类型更改为 string

[DataContract]
public class Expense
{
    [DataMember(Name = "account_name")]
    public string Account_Name { get; set; }
    [DataMember(Name = "paid_through_account_name")]
    public string Paid_Through_Account_Name { get; set; } //string type
}

更新

可能您正在尝试从另一个(非主)线程更新 UI。在 Windows Phone 应用程序中,您只能在主线程中更新UI。在这种情况下,请使用此类构造调度程序。

 Dispatcher.BeginInvoke(() =>
                    {
                          TextBox.Text = myString;
                    });

http://msdn.microsoft.com/en-us/library/ms741870.aspx

http://weimenglee.blogspot.ru/2013/07/windows-phone-tip-updating-ui-from.html