将JSON对象转换为int和对象

问题描述:

我的android应用程序正在与一个webservice进行通信,该webservice在一个JSONObject,一个整数和一个对象中返回2个值。但不知何故,我似乎无法将它们转换回int和一个对象。将JSON对象转换为int和对象

我与我的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(); 
     } 
    } 
} 

我希望你们看到的问题。在此先感谢

+0

什么是你的问题?解析{“d”:{“__ type”:“Datalayer.User”,“med_ID”:24,“geb_GUID”:“8bb4894b-92f7-45af-9a40-b99d7a06a506”}}? – Blackbelt 2013-05-06 15:02:45

+0

是的,这是我的问题确实 – user 2013-05-07 08:05:32

试试这个代码 - >

JSONObject dObject = output.getJSONObject("d") 

this.UserID = dObject.getInt("med_ID"); 
this.gebGUID = dObject.getString("geb_GUID"); // As your geb_GUID is String object 
+0

非常感谢,它解决了我的问题 – user 2013-05-07 08:06:07

+0

虽然它不是this.UserID = dObject.getInt(“d”),但我将不得不用“med_ID”替换“d” – user 2013-05-07 09:21:00

+0

哦yess ...我的不好,我会编辑我的答案 – 2013-05-07 09:45:05

如果你想JSON字符串转换成一个对象,你可以使用:

Type type = new TypeToken<HashMap<String, ArrayList<String>>>(){}.getType(); 
HashMap<String, ArrayList<String>> result = new Gson().fromJson(json, type); 

你可以改变的类型,在我的情况,这是一个HashMap<String, ArrayList<String>>