Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.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
如何解析这种类型的JSON?(只能迭代java.lang.Iterable的数组或实例)_Java_Json - Fatal编程技术网

如何解析这种类型的JSON?(只能迭代java.lang.Iterable的数组或实例)

如何解析这种类型的JSON?(只能迭代java.lang.Iterable的数组或实例),java,json,Java,Json,我试图解析下面的一些JSON摘录,但收到以下错误: Exception in thread "main" java.lang.Error: Unresolved compilation problem: Can only iterate over an array or an instance of java.lang.Iterable 还有一个非常大的json文件的摘录: Exception in thread "main" java.lang.Error: Unresolved

我试图解析下面的一些JSON摘录,但收到以下错误:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    Can only iterate over an array or an instance of java.lang.Iterable
还有一个非常大的json文件的摘录:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    Can only iterate over an array or an instance of java.lang.Iterable
[
  {
    "normal" : "Le tour change de sens ! Ce n'est pas une action, c'est le joker!"
  },
  {
    "normal" : "Prends la main de ton voisin de gauche"
  },
  {
    "normal" : "Fais une déclaration d'amour à la personne en face de toi!"
  },
  {
    "normal" : "Reste sur les genoux de la personne à ta droite pendant un tour"
  },
我自己想出来。代码如下:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    Can only iterate over an array or an instance of java.lang.Iterable
import org.json.simple.JSONArray;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import org.json.JSONException;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;


public class ParseJson{


    static String printAcounts = "";
    public static void main(String[] args) throws FileNotFoundException, IOException, ParseException, JSONException{
        JSONParser parser = new JSONParser();
        JSONArray objArray = (JSONArray) parser.parse(new FileReader("C:\\Users\\Bogdanel\\Desktop\\Ressources\\normal_actions.json"));

        for (Object o : objArray){
        JSONObject obj = (JSONObject) o;
        String s = (String) obj.get("normal");
        System.out.println(s);
        }


    }
}
asc

不要混淆org.json.JSONArray和javax.json.JSONArray。前者不实现Iterable,而后者实现

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    Can only iterate over an array or an instance of java.lang.Iterable
Android对JSONArray的使用就像典型的JSON值一样,只是它可以像集合一样获取索引。JsonArray基本上是一个列表

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    Can only iterate over an array or an instance of java.lang.Iterable

简而言之,使用一个典型的for结构,而使用get。

JSONArray与常规数组不同,因此不能使用Java中的foreach高级for循环对其进行迭代。您可以参考这一点,但基本上使用一个常规for循环并获得如下项目

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    Can only iterate over an array or an instance of java.lang.Iterable
objArray.getJSONObject(i).getString("someKey");

您使用的是哪个JSON库?您拥有的类JSONArray没有实现Iterable,因此您不能使用Java的extended for循环语法。解决方案是在JSONArray上使用这些方法。例如,如果它具有长度、大小或计数以及get方法。这将取决于您使用的确切json库。