将JSONobjects转换为int和object

时间:2013-05-06 14:52:29

标签: android

我的Android应用程序正在与一个Web服务进行通信,该服务在一个JSONObject,一个整数和一个对象中返回2个值。但不知何故,我似乎无法将它们转换回一个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();
        }
    }
}

我希望你们看到这个问题。提前致谢

2 个答案:

答案 0 :(得分:1)

试试此代码 - >

JSONObject dObject = output.getJSONObject("d")

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

答案 1 :(得分:0)

如果要将json字符串转换为对象,可以使用:

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

您可以更改类型,在我的情况下,它是HashMap<String, ArrayList<String>>

相关问题