我希望有人可以帮助我,因为我真的在为此努力。
基本上我正在寻找的是能够从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中执行此操作?什么是最好的解析器呢?
谢谢
答案 0 :(得分:0)
我建议你使用Jackson或Gson mapper。
创建一个代表所需对象的Java类:
[keychainItem setObject:[[NSUUID UUID] UUIDString] forKey:(id)kSecAttrService];
然后将你的json String映射到你的对象(这是Jackson的一个例子):
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;
}
}
答案 1 :(得分:0)
您正在尝试获取jsonObject.get("fonction1")
之类的JSONObject,这会返回null
值,因此当您在其上调用.size()
方法时,您将获得NullPointerException
1}}。
现在null
值的解释是您的JSON不包含具有键fonction1
的对象。只需用密钥fonction
替换它,它应该可以正常工作。代码必须更改为
JSONArray lang = (JSONArray)jsonObject.get("fonction");