How to convert Object to NameValuePair ArrayList in Java?

7.6k Views Asked by At

Lets say we have an object of Class Person with all String parameters :

Class Person {

 String name;
 String email;

}

Person p = new Person();
p.name = "ABC";
p.email = "[email protected]";

How do we convert object p to ArrayList of NameValue Pair :

Output should be a ArrayList<NameValuePair> with following content :

"name" : "ABC" 
"email" : "[email protected]"
4

There are 4 best solutions below

0
On BEST ANSWER

Here you are .. it's a dynamic so you can use it for any object .. I'm using the reflection

public static void main(String[] args) {

    Person person = new Person("Ali", "[email protected]");
    try {
        System.out.println(getObjectNameValuePairs(person));

    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

public static ArrayList<NameValuePair> getObjectNameValuePairs(Object obj) throws IllegalArgumentException, IllegalAccessException {
    ArrayList<NameValuePair> list = new ArrayList<NameValuePair>();
    for (Field field : obj.getClass().getDeclaredFields()) {
        field.setAccessible(true); // if you want to modify private fields
        NameValuePair nameValuePair = new NameValuePair();
        nameValuePair.setName(field.getName());
        nameValuePair.setValue(field.get(obj));
        list.add(nameValuePair);
    }
    return list;
}

this output will be

[[name:Ali], [email:[email protected]]]

considering that the NameValuePair implementation is

public class NameValuePair {
private String name;
private Object value;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public Object getValue() {
    return value;
}

public void setValue(Object value) {
    this.value = value;
}

@Override
public String toString() {
    return "["+ name + ":" + value + "]";
}

}

I hope this could help!

1
On

Following way you can do it.

Map<String, String> map = new HashMap<String, String>();
map.put(p.name, p.email);

or you could do like this

Map<String, Person> map = new HashMap<String, Person>();
map.put(p.name, p);
0
On

Add in Person a get method:

public NameValuePair getAsNameValuePair() {
    NameValuePair pair=new BasicNameValuePair(this.getName,this.getEmail);
    return pair;
}

If you want to store the person in an array:

public List<NameValuePair> getAsNameValuePair() {
    List<NameValuePair> pairs = new ArrayList<NameValuePair>();
    pairs.add(new BasicNameValuePair("name", this.getName);
    pairs.add(new BasicNameValuePair("email", this.getEmail);
    return pairs;
}

But maybe is better option to use a Map instead...

0
On

You can add array list to name value pair as mentioned in the code.Just convert array list to string by .toString() method.

    kk = new ArrayList<NameValuePair>();
    kk.add(new BasicNameValuePair("task_id",task_id.toString()));
    kk.add(new BasicNameValuePair("task_status",task_status.toString()));
    kk.add(new BasicNameValuePair("task_remarks",task_remarks.toString()));
    kk.add(new BasicNameValuePair("task_rca",task_rca.toString()));


kk.add(newBasicNameValuePair("equip_serial_no",equip_serial_no.toString()));