user
user

Reputation: 121

Convert JSONobjects to int and object

My android app is communicating with a webservice which returns to 2 values in one JSONObject, an integer and an object. But somehow I just can't seem to convert them to back to an int and an object.

The value I am retrieving with my JSONObject:

{"d":{"__type":"Datalayer.User","med_ID":24,"geb_GUID":"8bb4894b-92f7-45af-9a40-b99d7a06a506"}}

and this is the code I am using:

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

I hope that you guys see the problem. Thanks in advance

Upvotes: 0

Views: 1388

Answers (2)

Vishal Pawale
Vishal Pawale

Reputation: 3476

Try this code ->

JSONObject dObject = output.getJSONObject("d")

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

Upvotes: 1

Brtle
Brtle

Reputation: 2297

If you want to convert a json string into an object, you can use :

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

You can change the type, in my case it was a HashMap<String, ArrayList<String>>.

Upvotes: 0

Related Questions