Skip to content Skip to sidebar Skip to footer

How To Get Values From Users Objec?

Hi in the below code how to get names of the below JSON responses from users object.want to display the names and setting to the spinner adapter. can anyone help me expected output

Solution 1:

Lets say your JSON data is in String

String jsondata ;
JSONObject jsonobj = newJSONObject(jsondata);

Since this json object has many properties so we will get the property "type" which is of type Object, So we will create another object of JSONObject

JSONObjecttype = jsonobj.getJSONObject("type");

Inside this type json object we have two properties name and users and in which user is another object, so we will get users object from type object

JSONObject usr = jsonobj.getJSONObject("users");
Iterator<String> keys = usr.keys();
while(keys.hasNext()) {
    String key = keys.next();
    if (jsonObject.get(key) instanceofJSONObject) {
        //print here      
    }
}


//   String n1 = usr.getString("19x1");// String n2 = usr.getString("19x5");// String n3 = usr.getString("19x6");//and So on......

Create XML for Spinner Android

<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"  ><Spinnerandroid:id="@+id/spinner"android:layout_width="149dp"android:layout_height="40dp"android:layout_marginBottom="8dp"android:layout_marginEnd="8dp"android:layout_marginStart="8dp"android:layout_marginTop="8dp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintHorizontal_bias="0.502"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent"app:layout_constraintVertical_bias="0.498" />

And the MainActivity code is

publicclassMainActivityextendsAppCompatActivityimplementsAdapterView.OnItemSelectedListener {  
String[] names = { n1,n2,n3};  

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.activity_main);  
   //Getting the instance of Spinner and applying OnItemSelectedListener on it  Spinnerspin= (Spinner) findViewById(R.id.spinner);  
    spin.setOnItemSelectedListener(this);  

    //Creating the ArrayAdapter instance having the  list  ArrayAdapteraa=newArrayAdapter(this,android.R.layout.simple_spinner_item,country);  
    aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);  
    //Setting the ArrayAdapter data on the Spinner  
    spin.setAdapter(aa);  

}  

//Performing action onItemSelected and onNothing selected  @OverridepublicvoidonItemSelected(AdapterView<?> arg0, View arg1, int position, long id) {  
    Toast.makeText(getApplicationContext(),names[position] , Toast.LENGTH_LONG).show();  
}  
@OverridepublicvoidonNothingSelected(AdapterView<?> arg0) {  
    // TODO Auto-generated method stub  
}

Solution 2:

String response ="{\n"+"  \"name\": \"assigned_user_id\",\n"+"  \"label\": \"Assigned To\",\n"+"  \"mandatory\": true,\n"+"  \"type\": {\n"+"    \"name\": \"owner\",\n"+"    \"users\": {\n"+"      \"19x1\": \"Admin Administrator\",\n"+"      \"19x5\": \"Ganeshprasad S\",\n"+"      \"19x6\": \"Balaji RR\",\n"+"      \"19x7\": \"Kiran Thadimarri\",\n"+"      \"19x8\": \"Sridhar Balakrishnan\",\n"+"      \"19x9\": \"Shilpa MK\",\n"+"      \"19x10\": \"Velmurugan N\",\n"+"      \"19x11\": \"Aamir Khanna\",\n"+"      \"19x12\": \"Jamir Abbas Pinjari\",\n"+"      \"19x13\": \"Syed Shadab Ashraf\",\n"+"      \"19x14\": \"Shahul Hameed\",\n"+"      \"19x15\": \"Manjula C\",\n"+"      \"19x16\": \"Keerthi Vasan L\",\n"+"      \"19x17\": \"Lochan Jyoti Borgohain\",\n"+"      \"19x18\": \"Rajkumar Sanatomba Singh\",\n"+"      \"19x19\": \"Krishna Pandey\",\n"+"      \"19x20\": \"Nabajit Pathak\",\n"+"      \"19x21\": \"Manoranjan Ningthoujam\",\n"+"      \"19x22\": \"Pravin Karbhari Ahire\",\n"+"      \"19x23\": \"Pratap Kumar Choudhary\"\n"+"    }\n"+"  }\n"+"}";

This the data class

publicclassUser {

    privateString id;
    privateString name;

    publicUser(String id, String name) {
        this.id = id;
        this.name = name;
    }

    publicStringgetId() {
        return id;
    }

    publicStringgetName() {
        return name;
    }


    @OverridepublicStringtoString() {
        return name;
    }

    @Overridepublicbooleanequals(Object obj) {
        if(obj instanceofUser){
            User user = (User )obj;
            if(user.getName().equals(name) && user.getId()==id ) returntrue;
        }

        returnfalse;
    }
}

UserArrayList

This is how you can get the data

ArrayList<User> userList = newArrayList<>();

try {
            JSONObject jsonobj = newJSONObject(response);
            JSONObjecttype = jsonobj.getJSONObject("type");
            JSONObject usr = type.getJSONObject("users");
            Iterator<String> keys = usr.keys();
            while(keys.hasNext()) {
                String key = keys.next();

                    String value = usr.getString(key);
                    User user = newUser(key,value);
                    userList.add(user);
            }

            Log.e("value","value----"+userList);
        } catch (JSONException e) {
            e.printStackTrace();
        }

This is the spinner Adapter

ArrayAdapter<User> adapter = newArrayAdapter<User>(context, android.R.layout.simple_spinner_dropdown_item, userList );
        spinneraccountManager.setAdapter(adapter);

        spinneraccountManager.setOnItemSelectedListener(newAdapterView.OnItemSelectedListener() {
            @OverridepublicvoidonItemSelected(AdapterView<?> parent, View view, int position, long id) {

                Useruser= (User) parent.getSelectedItem();
                Toast.makeText(view.getContext(), "User ID: "+user.getId()+",  User Name : "+user.getName(), Toast.LENGTH_SHORT).show();
            }

            @OverridepublicvoidonNothingSelected(AdapterView<?> parent) {
            }
        });

Post a Comment for "How To Get Values From Users Objec?"