【问题标题】:Trouble with json parsing for windows phonewindows phone 的 json 解析问题
【发布时间】:2013-10-16 05:28:56
【问题描述】:
  {
   "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.

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

【问题讨论】:

    标签: c# json windows-phone-7 json-deserialization


    【解决方案1】:

    您有 json "paid_through_account_name": "Petty Cash",其中 "Petty Cash" 是 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。

     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

    【讨论】:

    • 当我尝试将内容放入 TextBox 时,我收到一条错误消息,指出“System.UnauthorizedAccessException”:无效的跨线程访问。我该怎么做才能纠正这个问题??
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-11
    • 2012-02-22
    相关资源
    最近更新 更多