lundi 17 septembre 2018

Read JSON Nested element using reflection NOT JsonFactoy and ObjectMapper

[Unable to access property of another object stored in Arraylist] I am creating an function to get JSON input in object from RESTful Web service input and format it again in JSON format to call other web service.

I have limitation that I can not use any JSON API for object mapping hence using Java reflection core API.

I am able to create JSON format from Input for simple elements but unable to access nested elements (another user defined POJO class ). I am using arraylist.

Input

{
  "GenesisIncidents": {
"service": "Transmission",
"affectedCI": "22BT_ORNC03",
"opt_additionalAffectedItems": [
   {
      "itemType": "NODE-ID",
      "ItemName": "22BT_ORNC03"
   },
   {
      "ItemType": "CCT",
      "ItemName": "A_circuit_id"
        }]   
}
}

GenesisIncidents.class

import java.util.ArrayList;
import java.util.Date;
public class GenesisIncidents {

private String service;
private String affectedCI;
private ArrayList<AdditionalAffectedItems> opt_additionalAffectedItems;

public GenesisIncidents(){}

public String getService() {
    return service;
}

public void setService(String service) {
    this.service = service;
}

public String getAffectedCI() {
    return affectedCI;
}

public void setAffectedCI(String affectedCI) {
    this.affectedCI = affectedCI;
}


public ArrayList<AdditionalAffectedItems> getOpt_additionalAffectedItems() {
    return opt_additionalAffectedItems;
}
public void setOpt_additionalAffectedItems(ArrayList<AdditionalAffectedItems> opt_additionalAffectedItems) {
    this.opt_additionalAffectedItems = opt_additionalAffectedItems;
}
}

AdditionalAffectedItems.class

public class AdditionalAffectedItems {

private String itemType;
private String itemName;

public AdditionalAffectedItems(){
    super();
}

public String getItemType() {
    return itemType;
}

public void setItemType(String itemType) {
    this.itemType = itemType;
}

public String getItemName() {
    return itemName;
}

public void setItemName(String itemName) {
    this.itemName = itemName;
}
}

Implemetation

public void updateTicketExt(GenesisIncidents genesisIncidents) {
        try{

            Field allFields[]=genesisIncidents.getClass().getDeclaredFields();
            Method allMethods[] = genesisIncidents.getClass().getDeclaredMethods();
            String jsonString ="{\r\n \""+genesisIncidents.getClass().getName().toString().substring(48)+"\": {";
            final String preStr="\r\n  \"";             //To create a JSON object format.
            final String postStr="\": ";                //To create a JSON object format.
            int totalNoOfFields=allFields.length;

            for (Field field : allFields) {
                System.out.println(field.getType());
                String getter="get"+StringUtils.capitalize(field.getName());
                Method method= genesisIncidents.getClass().getMethod(getter, null);
                try{

                        if(field.getType().toString().contains("Integer"))
                            jsonString=jsonString + preStr + field.getName() + postStr +method.invoke(genesisIncidents).toString()+",";
                        else
                            jsonString=jsonString + preStr + field.getName() + postStr +"\""+method.invoke(genesisIncidents).toString()+"\",";

                        if(field.getType().toString().contains("ArrayList")){
                            System.out.println("ArrayListElement found");
                            genesisIncidents.getOpt_additionalAffectedItems().forEach(obj->{System.out.println(obj.getItemName());});
                            //convertArrayToJSON(field, genesisIncidents);
                        }

                }catch(NullPointerException npe)
                {
                    System.out.println("Null value in field.");
                    continue;
                }
                }
                jsonString=jsonString.substring(0,jsonString.length()-1);
                jsonString=jsonString+"\r\n }\r\n }";
                System.out.println("\n"+jsonString);
            }catch(Exception jex){
            jex.printStackTrace();
        }

    }

My below code line is unable to access object stored under array list.

genesisIncidents.getOpt_additionalAffectedItems().forEach(obj->{System.out.println(obj.getItemName());});

OUTPUT

karaf@root>class java.lang.String
class java.lang.String
class java.lang.String
class java.util.ArrayList
ArrayListElement found
null
null
{
 "GenesisIncidents": {
  "service": "Transmission",
  "affectedCI": "22BT_ORNC03",
  "opt_additionalAffectedItems": "    [org.apache.servicemix.examples.camel.rest.model.AdditionalAffectedItems@5881a    895,     org.apache.servicemix.examples.camel.rest.model.AdditionalAffectedItems@399b4e    eb]"
 }
 }





Aucun commentaire:

Enregistrer un commentaire