私は自分のプロジェクトを開発したとき、私はASPのWebメソッドから返さASPからデシリアライズ列JSONに
JSON文字列が正しく返さあるアンドロイド活動に誤りがあったが、クラスに改宗JSONクラスが等しいnullでは、私はクラスにJSON文字列
asp.netのWebメソッドは、Android方法
に有効なJSON文字列を返す変換するときandroid code:
//to call my service and get data
ServiceCall call = new ServiceCall();
String jsonuserdata = call.getUserData("[email protected]")
.toString();
Toast.makeText(getApplicationContext(), jsonuserdata,Toast.LENGTH_SHORT).show();
//this Toast message is print data correctly
Gson gson = new Gson();
Type userType = new TypeToken<UserData>(){}.getType();
UserData user = gson.fromJson(jsonuserdata,userType);
Toast.makeText(getApplicationContext(), "" + user.userid.toString(),
Toast.LENGTH_SHORT).show();
問題がある(私はGoogleのJSONジャー2.1でそれを変換します) - >
UserData user = gson.fromJson(jsonuserdata, UserData.class);
EDIT追加説明
Type userType = new TypeToken<UserData>(){}.getType();
UserData user = gson.fromJson(jsonuserdata,userType);
:
[WebMethod]
public string getUserData(string email)
{
UserData user = new UserData();
user = DataBase.getUserData(email);
JavaScriptSerializer js = new JavaScriptSerializer();
string strJSON = js.Serialize(user);
return strJSON;
}
が、これはあなたがしてクラスのインスタンスを簡単に解析することができます
public class UserData {
public static Class<UserData> USERDATA_CLASS = UserData.class;
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
String username;
String email;
String password;
String userid;
public UserData() {
}
public UserData(String id, String name, String age, String weight,
String height, String calorie, String status, String gender,
String idealweight, String minidealweight, String maxidealweight,
String email, String password, String country, String report,String accountstatus) {
this.userid = id;
this.username = name;
this.email = email;
this.password = password;
}
public UserData(UserData user) {
this.userid = user.userid;
this.username = user.username;
this.email = user.email;
this.password = user.password;
}
}
本当に、私は試してみます – Developer
{"username": "user"、 "email": "[email protected]"、 "password": "123"、 "userid": "100"}これはjsonです文字列 – Developer