0

I hope someone can help me out as I am really struggling for this.

Basically what I am looking for is to be able to extract data properly from a json file. the contents of my file is:

{ "fonction": [

{
    "nom":"f1 task3",
    "period":"150",
    "execution_time":"3",
    "weight":"4",
    "nb_inst":"22",
    "proba":"0.2",
    "cout_comm":"8",
    "destination":"f2",
    "nom_cond":"",
    "nom_fct":""
},

{
"nom":"f1 task3",
"period":"150",
"execution_time":"3",
"weight":"4",
"nb_inst":"22",
"proba":"0.2",
"cout_comm":"4",
"destination":"f3",
"nom_cond":"",
"nom_fct":""
},

{
"nom":"f1 task3",
"period":"150",
"execution_time":"3",
"weight":"4",
"nb_inst":"22",
"proba":"0.5",
"cout_comm":"12",
"destination":"f4",
"nom_cond":"",
"nom_fct":""
},

{
"nom":"f2 task3",
"period":"200",
"execution_time":"3",
"weight":"2",
"nb_inst":"21",
"proba":"0.1",
"cout_comm":"10",
"destination":"f5",
"nom_cond":"",
"nom_fct":""
},

{
"nom":"f2 task3",
"period":"200",
"execution_time":"3",
"weight":"2",
"nb_inst":"21",
"proba":"0.9",
"cout_comm":"2",
"destination":"f6",
"nom_cond":"",
"nom_fct":""
},

{
"nom":"f3 task3",
"period":"210",
"execution_time":"5",
"weight":"5",
"nb_inst":"16",
"proba":"0.3",
"cout_comm":"7",
"destination":"f6",
"nom_cond":"inclusion",
"nom_fct":"f1"
},

{
"nom":"f3 task3",
"period":"210",
"execution_time":"5",
"weight":"5",
"nb_inst":"16",
"proba":"0.7",
"cout_comm":"9",
"destination":"f7",
"nom_cond":"inclusion",
"nom_fct":"f1"
},

{
"nom":"f4 task3",
"period":"180",
"execution_time":"5",
"weight":"6",
"nb_inst":"25",
"proba":"0.6",
"cout_comm":"6",
"destination":"f7",
"nom_cond":"inclusion",
"nom_fct":"f1"
},

{
"nom":"f4 task3",
"period":"180",
"execution_time":"5",
"weight":"6",
"nb_inst":"25",
"proba":"0.4",
"cout_comm":"6",
"destination":"f8",
"nom_cond":"inclusion",
"nom_fct":"f1"
},

{
"nom":"f5 task3 ",
"period":"190",
"execution_time":"5",
"weight":"3",
"nb_inst":"12",
"proba":"0",
"cout_comm":"0",
"destination":"",
"nom_cond":"",
"nom_fct":""
},

{
"nom":"f6 task3",
"period":"210",
"execution_time":"4",
"weight":"1",
"nb_inst":"23",
"proba":"0.9",
"cout_comm":"7",
"destination":"f5",
"nom_cond":"exclusion",
"nom_fct":"f3"
},

{
"nom":"f6 task3",
"period":"210",
"execution_time":"4",
"weight":"1",
"nb_inst":"23",
"proba":"0.1",
"cout_comm":"4",
"destination":"f7",
"nom_cond":"exclusion",
"nom_fct":"f3"
},

{
"nom":"f7 task3",
"period":"220",
"execution_time":"1",
"weight":"5",
"nb_inst":"16",
"proba":"0",
"cout_comm":"0",
"destination":"",
"nom_cond":"exclusion",
"nom_fct":"f3"
},

{
"nom":"f8 task3",
"period":"260",
"execution_time":"4",
"weight":"4",
"nb_inst":"19",
"proba":"0",
"cout_comm":"0",
"destination":"",
"nom_cond":"",
"nom_fct":""
} ], "proc": [

{
"id":"1",
"wight_max":"40",
"frequency":"250",
"voltage":"1.2",
"nb_inst_cycle":"3",
"energy_cycle":"6",
"energy":"214"
},

{
"id":"2",
"wight_max":"40",
"frequency":"300",
"voltage":"1.32",
"nb_inst_cycle":"3",
"energy_cycle":"6",
"energy":"214"
},

{
"id":"3",
"wight_max":"40",
"frequency":"400",
"voltage":"1.7",
"nb_inst_cycle":"3",
"energy_cycle":"7",
"energy":"214"
}]}  

I found many tutorials/guide but nothing that could help me really.

How do I do this in java? and what is the best parser to do this?

Thank you

Jamél Samar
  • 3
  • 1
  • 3

2 Answers2

0

I would suggest you to use Jackson or Gson mapper.

Create a Java Class which represent the object you want :

  public class Fonction {

    private String nom;

    private Integer period;

    ..... All your parameters with getters and setters

    /**
     * @return the nom
     */
    public final String getNom() {
        return nom;
    }

    /**
     * @param pNom the nom to set
     */
    public final void setNom(String pNom) {
        nom = pNom;
    }

    /**
     * @return the period
     */
    public final Integer getPeriod() {
        return period;
    }

    /**
     * @param pPeriod the period to set
     */
    public final void setPeriod(Integer pPeriod) {
        period = pPeriod;
    }    
}

Then map your json String to your Object (This is an example with Jackson) :

ObjectMapper mapper = new ObjectMapper();

//JSON from file to Object
Fonction fonction= mapper.readValue(new File("c:\\fonction.json"), Fonction.class);

//JSON from String to Object
Fonction fonction= mapper.readValue(fonctionJsonString, Fonction.class);
Akah
  • 1,890
  • 20
  • 28
0

You are trying to get the JSONObject like jsonObject.get("fonction1"), this will return you a null value, and so when you are calling the .size() method on it you are getting a NullPointerException.

Now the explanation for the null value is that your JSON doesn't contain a object with the key fonction1. Just replace it with the key fonction and it should work fine. The code will have to be changed to

JSONArray lang = (JSONArray)jsonObject.get("fonction");
Shiva
  • 6,677
  • 4
  • 36
  • 61