2013-10-16 64 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