Java 如何从文件解析JSON数组?
我从文件中得到了JSONJava 如何从文件解析JSON数组?,java,json,Java,Json,我从文件中得到了JSON { "from_excel":[ { "solution":"Fisrt", "num":"1" }, { "solution":"Second", "num":"2" }, { "solution":"third", "num":"3" }, { "solution":"fourth", "
{
"from_excel":[
{
"solution":"Fisrt",
"num":"1"
},
{
"solution":"Second",
"num":"2"
},
{
"solution":"third",
"num":"3"
},
{
"solution":"fourth",
"num":"4"
},
{
"solution":"fifth",
"num":"5"
}
]
}
并希望解析解决方案列表和Num
使用liborg.json.simple.
试试这个
Object obj = parser.parse(new FileReader("E:\\json.txt"));
JSONObject jsonObject = (JSONObject) obj;
out.println(jsonObject.get("from_excel"));
JSONObject obj_new = (JSONObject) jsonObject.get("from_excel");
JSONArray solution = (JSONArray) obj_new.get("solution");
Iterator iterator = solution.iterator();
while (iterator.hasNext()) {
out.println(iterator.next());
}
Object obj=parser.parse(新文件读取器(“E:\\json.txt”);
JSONObject JSONObject=(JSONObject)对象;
out.println(jsonObject.get(“from_excel”);
JSONObject obj_new=(JSONObject)JSONObject.get(“来自excel”);
JSONArray solution=(JSONArray)obj_new.get(“solution”);
迭代器迭代器=solution.Iterator();
while(iterator.hasNext()){
out.println(iterator.next());
}
我做错了什么
如果你想写
JSONObject solutions = (JSONArray) jsonObject.get("from_excel");
JSONObject solutions=(JSONArray)JSONObject.get(“来自excel”);
如果你想写
JSONArray array = obj.getJSONArray("from_excel");
JSONArray数组=obj.getJSONArray(“来自excel”);
出错
来自excel的JSON数组不是对象。所以你应该实现它是数组
JSONObject jsonObject = (JSONObject) obj;
JSONArray array = obj.getJSONArray("from_excel");
然后迭代数组并获取每个jsonobject。像下面这样的
for(int i = 0 ; i < array.length() ; i++){
array.getJSONObject(i).getString("solution");
}
for(int i=0;i
来自excel的JSON数组不是对象。所以你应该实现它是数组
JSONObject jsonObject = (JSONObject) obj;
JSONArray array = obj.getJSONArray("from_excel");
然后迭代数组并获取每个jsonobject。像下面这样的
for(int i = 0 ; i < array.length() ; i++){
array.getJSONObject(i).getString("solution");
}
for(int i=0;i
JSON中没有“解决方案”数组。“from_excel”是数组。因此,您的代码应该如下所示:
Object obj = parser.parse(new FileReader("E:\\json.txt"));
JSONObject jsonObject = (JSONObject) obj;
out.println(jsonObject.get("from_excel"));
JSONArray solutions = (JSONArray) jsonObject.get("from_excel");
Iterator iterator = solutions.iterator();
while (iterator.hasNext()) {
out.println(iterator.next());
}
JSON中没有“解决方案”数组。“from_excel”是数组。因此,您的代码应该如下所示:
Object obj = parser.parse(new FileReader("E:\\json.txt"));
JSONObject jsonObject = (JSONObject) obj;
out.println(jsonObject.get("from_excel"));
JSONArray solutions = (JSONArray) jsonObject.get("from_excel");
Iterator iterator = solutions.iterator();
while (iterator.hasNext()) {
out.println(iterator.next());
}
工作溶液
JSONParser parser = new JSONParser();
JSONObject jsonObject;
try {
jsonObject = (JSONObject) parser.parse(new FileReader("E:\\json.txt"));
out.println("<br>"+jsonObject);
JSONArray from_excel = (JSONArray)jsonObject.get("from_excel");
// for row output 1
for(Object o: from_excel){
out.println("<br>"+o);
}
// for row output 2
Iterator iterator = from_excel.iterator();
while (iterator.hasNext()) {
out.println("<br>"+iterator.next());
}
// for item output 3
for (int i = 0; i < from_excel.size(); i++) {
JSONObject jsonObjectRow = (JSONObject) from_excel.get(i);
String num = (String) jsonObjectRow.get("num");
String solution = (String) jsonObjectRow.get("solution");
out.println("<br>num="+num+"; solution="+solution);
}
} catch (Exception e) {
out.println("Error: "+e);
}
JSONParser=newjsonparser();
JSONObject JSONObject;
试一试{
jsonObject=(jsonObject)parser.parse(新文件读取器(“E:\\json.txt”);
out.println(“
”+jsonObject);
JSONArray from_excel=(JSONArray)jsonObject.get(“from_excel”);
//对于行输出1
用于(对象o:来自excel){
out.println(“
”+o);
}
//对于行输出2
迭代器迭代器=来自_excel.Iterator();
while(iterator.hasNext()){
out.println(“
”+迭代器.next());
}
//关于项目产出3
对于(int i=0;inum=“+num+”;solution=“+solution”);
}
}捕获(例外e){
out.println(“错误:+e”);
}
工作解决方案
JSONParser parser = new JSONParser();
JSONObject jsonObject;
try {
jsonObject = (JSONObject) parser.parse(new FileReader("E:\\json.txt"));
out.println("<br>"+jsonObject);
JSONArray from_excel = (JSONArray)jsonObject.get("from_excel");
// for row output 1
for(Object o: from_excel){
out.println("<br>"+o);
}
// for row output 2
Iterator iterator = from_excel.iterator();
while (iterator.hasNext()) {
out.println("<br>"+iterator.next());
}
// for item output 3
for (int i = 0; i < from_excel.size(); i++) {
JSONObject jsonObjectRow = (JSONObject) from_excel.get(i);
String num = (String) jsonObjectRow.get("num");
String solution = (String) jsonObjectRow.get("solution");
out.println("<br>num="+num+"; solution="+solution);
}
} catch (Exception e) {
out.println("Error: "+e);
}
JSONParser=newjsonparser();
JSONObject JSONObject;
试一试{
jsonObject=(jsonObject)parser.parse(新文件读取器(“E:\\json.txt”);
out.println(“
”+jsonObject);
JSONArray from_excel=(JSONArray)jsonObject.get(“from_excel”);
//对于行输出1
用于(对象o:来自excel){
out.println(“
”+o);
}
//对于行输出2
迭代器迭代器=来自_excel.Iterator();
while(iterator.hasNext()){
out.println(“
”+迭代器.next());
}
//关于项目产出3
对于(int i=0;inum=“+num+”;solution=“+solution”);
}
}捕获(例外e){
out.println(“错误:+e”);
}
其解决方案必须在lib“org.json.simple.*”上运行?我试着用不兼容的typesites解决方案在lib“org.json.simple.*”上运行时出错?我试着用不兼容的typesites解决方案在lib“org.json.simple.*”上运行时出错?我尝试并得到了关于无法解析方法的错误(在问题体中添加带有错误的图片),它的解决方案必须在lib“org.json.simple.*”上工作?我试图得到关于无法解决方法的错误(在问题正文中添加带有错误的图片)