Android 将JSONObject转换为int和object
我的android应用程序正在与一个webservice通信,它在一个JSONObject中返回两个值,一个整数和一个对象。但不知何故,我似乎无法将它们转换回int和object 使用我的Android 将JSONObject转换为int和object,android,Android,我的android应用程序正在与一个webservice通信,它在一个JSONObject中返回两个值,一个整数和一个对象。但不知何故,我似乎无法将它们转换回int和object 使用我的JSONObject检索的值: {"d":{"__type":"Datalayer.User","med_ID":24,"geb_GUID":"8bb4894b-92f7-45af-9a40-b99d7a06a506"}} 这是我正在使用的代码: public class TUser { // P
JSONObject
检索的值:
{"d":{"__type":"Datalayer.User","med_ID":24,"geb_GUID":"8bb4894b-92f7-45af-9a40-b99d7a06a506"}}
这是我正在使用的代码:
public class TUser {
// Publics
public int UserID;
public String Username;
public String Password;
public String License;
public String DeviceId;
public Boolean Authenticated;
public Object gebGUID;
SharedPreferences prefs;
// // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // //
public TUser()
{
this.UserID = -1;
this.Username = "";
this.Password = "";
this.License = "";
this.DeviceId = "123";
this.Authenticated = false;
this.gebGUID = null;
}
// // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // //
public void AuthenticateUser(Context context) {
// Vars
JSONObject inputObject = new JSONObject();
StringEntity seParams = null;
JSONObject output = null;
String result = "0";
//standaard waarde
this.Authenticated = false;
// Create JSONObject with all the needed parameters for this call
try {
inputObject.put("UserName", this.Username);
inputObject.put("Password", this.Password);
inputObject.put("License", this.License);
inputObject.put("DeviceId", this.DeviceId);
seParams = new StringEntity(inputObject.toString());
} catch (Exception e) {
TLogFile.appendLog("e", "log_tag", "Error creating object " + e.toString());
}
// Create the HTTPRequest
TWebserviceHelper h = new TWebserviceHelper();
result = h.ExecuteAction(context, seParams, "/Login", 10000, 10000);
//bij geen result gaan we ook niet parsen
if (result == "")
{
return;
}
//try parse the string to a JSON object
try{
output = new JSONObject(result);
}catch(JSONException e){
TLogFile.appendLog("e", "log_tag", "Error parsing data "+e.toString());
}
try {
this.UserID = output.getInt("d");
this.gebGUID = output.get("geb_GUID");
if (this.UserID != 0){
this.Authenticated = true;
}else{
this.Authenticated = false;
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
我希望你们能看到问题所在。提前感谢如果要将json字符串转换为对象,可以使用:
Type type = new TypeToken<HashMap<String, ArrayList<String>>>(){}.getType();
HashMap<String, ArrayList<String>> result = new Gson().fromJson(json, type);
Type Type=new-TypeToken(){}.getType();
HashMap result=new Gson().fromJson(json,类型);
您可以更改类型,在我的示例中,它是一个HashMap
尝试此代码->
JSONObject dObject = output.getJSONObject("d")
this.UserID = dObject.getInt("med_ID");
this.gebGUID = dObject.getString("geb_GUID"); // As your geb_GUID is String object
你有什么问题?解析{“d”:{“uuu类型”:“Datalayer.User”,“med_ID”:24,“geb_GUID”:“8bb4894b-92f7-45af-9a40-b99d7a06a506”}有问题吗?是的,这是我的问题,尽管不是这个。UserID=dObject.getInt(“d”),但我必须用“med”替换“d”