Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.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文件读取数据_Json_Selenium Webdriver_Filereader - Fatal编程技术网

无法从JSON文件读取数据

无法从JSON文件读取数据,json,selenium-webdriver,filereader,Json,Selenium Webdriver,Filereader,在尝试上面的代码时,我收到了下面的错误 java.lang.ClassCastException:org.json.simple.JSONArray不能强制转换为org.json.simple.JSONObject 位于testdata.main(testdata.java:33) 我的JSON文件 您可以将其解析为对象类型,然后检查您具有数组或对象的json结构类型 import java.io.FileNotFoundException; import java.io.FileReader;

在尝试上面的代码时,我收到了下面的错误

java.lang.ClassCastException:org.json.simple.JSONArray不能强制转换为org.json.simple.JSONObject 位于testdata.main(testdata.java:33)

我的JSON文件


您可以将其解析为对象类型,然后检查您具有数组或对象的json结构类型

import java.io.FileNotFoundException;
import java.io.FileReader;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

public class testdata {
    /**
     * @param args
     * @throws FileNotFoundException 
     */
    public static void main(String[] args) throws FileNotFoundException {
        JSONParser parser = new JSONParser(); 
        try { System.out.println("Reading JSON file from Java program"); 
        FileReader fileReader = new FileReader("C:\\Users\\...\\testdata.json"); 
        JSONObject json = (JSONObject) parser.parse(fileReader); 
        String title = (String) json.get("Attachment__c"); 
        System.out.println("title: " + title); 
                } catch (Exception ex) 
        { ex.printStackTrace(); }
}}
publicstaticvoidmain(字符串[]args)抛出FileNotFoundException{
JSONParser=新的JSONParser();
试一试{
FileReader FileReader=newfilereader(“C:\\Users\\Priya\\testdata.json”);
objectjsonobj=parser.parse(fileReader);
if(JSONObject的JSONObject实例){
}else if(jsonObj实例为JSONArray){
JSONArray数组=(JSONArray)jsonObj;
System.out.println(array.size());
对于(int i=0;i
我已经更新了我的问题。我需要从json文件中获取附件我需要从json文件中获取附件的值。我已经尝试了上面的代码,但仍然不起作用。我们可以和聊天室联系吗
System.out.println("Reading JSON file from Java program");
FileReader fileReader = new FileReader("C:\\Users\\...\\testdata.json");
Object jsonObj = parser.parse(fileReader);
if (jsonObj instanceof JSONObject) {
    // its an object
} else if (jsonObj instanceof JSONArray) {
    JSONArray array = (JSONArray) jsonObj;
    array.forEach(i -> {
        JSONObject obj = (JSONObject) i;
        JSONObject attributes = (JSONObject) obj.get("attributes");

        System.out.println(attributes.get("Attachment__c"));
   });
} else {
    // something else
}
public static void main(String[] args) throws FileNotFoundException {
    JSONParser parser = new JSONParser();
    try {
        FileReader fileReader = new FileReader("C:\\Users\\Priya\\testdata.json");
        Object jsonObj = parser.parse(fileReader);
        if (jsonObj instanceof JSONObject) {

        } else if (jsonObj instanceof JSONArray) {
            JSONArray array = (JSONArray) jsonObj;
            System.out.println(array.size());
            for (int i = 0; i < array.size(); i++) {
                String attachmentValue = (String) ((JSONObject) array.get(i)).get("Attachment__c");
        }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}