问:我正在尝试捕获在键“值”内捕获的值并将其显示在控制台上。但是当执行错误时“org.json.simple.JSONArray无法强制转换为org.json.simple.JSONObject”
代码编写:
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class FileExtractJSON
{
public static void main(String[] args) throws Exception
{
JSONParser parser = new JSONParser();
try
{
System.out.println("Reading JSON file from Java program");
FileReader fileReader = new FileReader("C:\\Users\\shisyadav\\Desktop\\HP Remainco\\JSON Objects\\directkafkaconsumer_actual.json");
JSONObject value_expected = (JSONObject) parser.parse(fileReader);
JSONArray jsonObject = (JSONArray) value_expected.get("value");
Iterator i = jsonObject.iterator();
System.out.println("Values:");
while (i.hasNext())
{
System.out.println(""+i.next());
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
Sample file from which we are trying to capture value against key "value"