json解析中发生异常

时间:2013-04-25 04:08:25

标签: json windows-phone-8

我正在尝试解析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

时发生异常

帮帮我怎样才能解决这个问题?

1 个答案:

答案 0 :(得分:0)

我得到了这个异常的解决方案。实际上我的json以整数形式返回值,我在字符串中进行类型转换,因此它给出了错误。

我的解决方案是:

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