2013-10-16 66 views
0
{ 
    "code": 0, 
    "message": "success", 
    "expense": { 
      "account_name": "Printing and Stationery", 
      "paid_through_account_name": "Petty Cash" 
       } 
    } 

这是我的JSON格式, 而且我试着用下面的类麻烦与JSON解析Windows Phone的

[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; } 
} 

反序列化它,我调用这个类与follwing的帮助代码

 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. 

帮助我走出这个问题....

回答

0

你有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。

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

+0

再次感谢@Alexandr ......这样做是正确! :) – Aju

+0

当我尝试将内容放入**文本框**我收到一个错误,指出'System.UnauthorizedAccessException':无效的跨线程访问。我应该怎样做才能纠正这个问题? – Aju

+0

我更新我的答案,检查它 – Alexandr