简体   繁体   English

Windows Phone的JSON解析出现问题

[英]Trouble with json parsing for windows phone

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

This is my json format, And i'm try to deserialize it with the following class 这是我的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; }
}

and i call this class with the help of the follwing code 我在下面的代码的帮助下调用该类

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

but while executing the above line i always get an error stating that, 但是在执行上述代码时,我总是会收到一条错误消息,指出

     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.

help me to get out of this issue.... 帮助我摆脱这个问题。

You have json "paid_through_account_name": "Petty Cash" where "Petty Cash" is string . 您有json “ paid_through_account_name”:“ Petty Cash”,其中“ Petty Cash”是string But in your model property public Paid_Through_Account_Name has type int . 但是在您的模型属性中,公共Paid_Through_Account_Name类型为int Try change type to string . 尝试将类型更改为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
}

Update 更新资料

Probably you are trying to update the UI from the other (not main) thread. 可能您正在尝试从其他(不是主)线程更新UI。 In Windows Phone app you can update UI only in main thread . 在Windows Phone应用中,您只能在主线程中更新UI In this case, use such construction Dispatcher. 在这种情况下,请使用此类构造分派器。

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM