Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从java中的JSON文件提取数据_Java_Json - Fatal编程技术网

从java中的JSON文件提取数据

从java中的JSON文件提取数据,java,json,Java,Json,我希望有人能帮助我,因为我真的在为此奋斗 基本上,我想要的是能够从json文件中正确提取数据。 我的文件的内容是: { "fonction": [ { "nom":"f1 task3", "period":"150", "execution_time":"3", "weight":"4", "nb_inst":"22", "proba":"0.2", "cout_comm":"8", "destination":"f2",

我希望有人能帮助我,因为我真的在为此奋斗

基本上,我想要的是能够从json文件中正确提取数据。 我的文件的内容是:

{ "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"
}]}  
我找到了很多教程/指南,但没有什么能真正帮助我

如何在java中实现这一点?最好的解析器是什么


谢谢

我建议您使用Jackson或Gson mapper

创建表示所需对象的Java类:

  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;
    }    
}
然后将json字符串映射到对象(这是Jackson的一个示例):


您正在尝试获取JSONObject,如
JSONObject.get(“Foction1”)
,这将返回一个
null
值,因此当您在其上调用
.size()
方法时,您将获得一个
NullPointerException

现在对
null
值的解释是,JSON不包含键为
fonction1
的对象。只需将其替换为键
功能
,即可正常工作。代码必须更改为

JSONArray lang = (JSONArray)jsonObject.get("fonction");

请发布实际代码,而不是屏幕截图。是的,我必须将“fonction 1”替换为“fonction”thanks@Jam艾尔萨马尔:如果这个答案对你有帮助,你可以点击答案左角的箭头符号来接受。
JSONArray lang = (JSONArray)jsonObject.get("fonction");