0

I have a CustomerEntiy with getter and setter the sample customerEntity is

customerEntity =
{
  "customerNumber": "1234",
  "firstName": "Test",
  "email": "[email protected]",
  "id": "1",
  "middleName": "doe",
  "phone": "11111"
}

I have java class Attributes of JsonProperty with getter and Setters as below

Attributes = 
{
"name": "string"
"value": "string"
}

I have a list which will contains random elements from CustomerEntiy for example:

List<String> stringlist = { "firstName" , "phone"}

I have created an List of type Attributes

List<Attributes> Attributeslist = new ArrayList<>();

I want to create Attributelist of all the elements in stringlist for Example here it would be:

Attributeslist =[
{
"name": "firstName"
"value": "Test"
},
{
"name": "phone"
"value": "11111"
}
]

For this i have written the code as below but what to pass in previewattributes.setValue(); because that value would depends on what is mystring in below for loop. In this example it would be previewattributes.setValue(customerEntity.getFirstName()); and previewattributes.setValue(customerEntity.getPhone()); for different iteration but how do i code it??

for (String mystring : stringlist) {
    Attributes previewattributes;
    previewattributes = new Attributes();
    previewattributes.setName(mystring);
    previewattributes.setValue(value here would be of customerentity);
    Attributeslist.add(previewattributes);
}

CustomerEntity:

@Entity
@Table(name = "Customer_tbl")
public class CustomerEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long rowId;


    @Column(name = "customer_number")
    private String customerNumber;

    public String getCustomerNumber() {
        return customerNumber;
    }

    public void setCustomerNumber(String customerNumber) {
        this.customerNumber = customerNumber;
    }
}
5
  • Depends on what data structure you use to store your customerentities.? You didn't mention that. Commented Oct 16, 2020 at 8:42
  • use reflection, get all fields and iterate with each fields. Commented Oct 16, 2020 at 8:54
  • you have an array of JSON of customer entities or a List ? Commented Oct 16, 2020 at 9:13
  • @Hades array of JSON Commented Oct 16, 2020 at 11:01
  • @yatharthmeena posted an answer is that what u wanted ? Commented Oct 16, 2020 at 13:09

2 Answers 2

1

You can make use reflection in Java. Even more simple way is to store the customerEntity in HashMap and fetch from it.

HashMap<String,String> m=new HashMap<>();
m.put("customerNumber", customerEntity.getCustomerNumber());
m.put("firstName", customerEntity.getFirstName());
m.put("email", customerEntity.getEmail());
m.put("id", customerEntity.getId());
m.put("middleName", customerEntity.getMiddleName());
m.put("phone", customerEntity.getPhone());

And inside for loop:

previewattributes.setValue(m.get(attribute));
Sign up to request clarification or add additional context in comments.

1 Comment

Storing as hashmap worked but how can i use reflection?
1

If I understood u correctly u have an Array of Customer Json in which u want some attribute separated , check below code

public static void main(String[] args)  {

    String jsonArray = "[\r\n" + 
            "   {\r\n" + 
            "      \"customerNumber\":\"1234\",\r\n" + 
            "      \"firstName\":\"Test\",\r\n" + 
            "      \"email\":\"[email protected]\",\r\n" + 
            "      \"id\":\"1\",\r\n" + 
            "      \"middleName\":\"doe\",\r\n" + 
            "      \"phone\":\"11111\"\r\n" + 
            "   },\r\n" + 
            "   {\r\n" + 
            "      \"customerNumber\":\"1235\",\r\n" + 
            "      \"firstName\":\"Test2\",\r\n" + 
            "      \"email\":\"[email protected]\",\r\n" + 
            "      \"id\":\"2\",\r\n" + 
            "      \"middleName\":\"doe2\",\r\n" + 
            "      \"phone\":\"2222\"\r\n" + 
            "   }\r\n" + 
            "]";
    
    List<String> requiredKeys = Arrays.asList("firstName" , "phone");
    JSONArray array = new JSONArray(jsonArray);
    Iterator iterator = array.iterator();
    
    JSONArray outputArray = new JSONArray(); 
    while(iterator.hasNext()) {
        JSONObject jsonObject = (JSONObject)iterator.next();
        Iterator<String> keys = jsonObject.keys();
        while(keys.hasNext()) {
            String key = keys.next();
            if(requiredKeys.contains(key)) {
                JSONObject attribute = new JSONObject();
                attribute.put("name", key);
                attribute.put("value", jsonObject.get(key));
                outputArray.put(attribute);
            }
        }
    }
    
    System.out.println(outputArray);
}

}

output

[
   {
      "name":"firstName",
      "value":"Test"
   },
   {
      "name":"phone",
      "value":"11111"
   },
   {
      "name":"firstName",
      "value":"Test2"
   },
   {
      "name":"phone",
      "value":"2222"
   }
]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.