2013-04-25 112 views
-1

我想解析json。我得到以下異常:在json解析中發生異常

An exception of type 'System.ArgumentException' occurred in Newtonsoft.Json.Silverlight.DLL but was not handled in user code 

我的C#代碼是發生

private void imgloginbtn_Tap_1(object sender, 
           System.Windows.Input.GestureEventArgs e) { 
    ///set flag in app.xaml for popup 
    var obj = App.Current as App; 
    obj.Popupflag = true; 
    //WhatsupServices.WhatsUpServiceSoapClient ws = 
        //new WhatsupServices.WhatsUpServiceSoapClient(); 
    WhatsupServices.WhatsUpServiceSoapClient ws = 
        new WhatsupServices.WhatsUpServiceSoapClient(); 
    ws.ChangePasswrdJsonCompleted += ws_ChangePasswrdJsonCompleted; 
    ws.ChangePasswrdJsonAsync("man", "man", "man"); 
} 
void ws_ChangePasswrdJsonCompleted(object sender, 
       WhatsupServices.ChangePasswrdJsonCompletedEventArgs e) { 
    string s = e.Result; 
    JObject obj = JObject.Parse(s); 
    string ResultCode = (string) obj["ResultCode"]; 
    string ResponceMessage = (string) obj["ResponseMessage"]; 
} 

例外,當我特林得到的ResultCode

幫幫我我怎樣才能解決這個問題?

+2

什麼'e.Result'的字符串值? – 2013-04-25 04:09:58

+0

你可能在你的json中有語法錯誤。請添加它。 – rekire 2013-04-25 04:11:17

+0

@AndreyShchekin:e.result = {「ResultCode」:0,「ResponseMessage」:「無效的密碼」} – MansinhDodiya 2013-04-25 04:14:18

回答

0

我得到了這個異常的解決方案。我的json返回整數值,我在字符串中進行類型轉換,因此會給出錯誤。

我的解決方案是:

string s = e.Result; 
    JObject obj = JObject.Parse(s); 
    int ResultCode = (int) obj["ResultCode"]; 
    string ResponceMessage = (string) obj["ResponseMessage"];