Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/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
Java (线程“main”org.json中的异常)JSONObject文本必须以';{';在字符1处_Java_Json - Fatal编程技术网

Java (线程“main”org.json中的异常)JSONObject文本必须以';{';在字符1处

Java (线程“main”org.json中的异常)JSONObject文本必须以';{';在字符1处,java,json,Java,Json,我一直很难读取目录中的json文件:E:\1PROGRAMMING\Padlock\src\Padlock\register\info.json { "users": [ { "Email": "3", "Second Name": "2", "Age": "123", "Name&quo

我一直很难读取目录中的json文件:E:\1PROGRAMMING\Padlock\src\Padlock\register\info.json

{
  "users": [
    {
      "Email": "3",
      "Second Name": "2",
      "Age": "123",
      "Name": "1",
      "Password": "4"
    }
  ]
}
问题是,每次我试图将我的内容文件作为json对象读取时,我都会收到一个错误,比如帖子的标题。 主要思想是读取这个json文件,在我的json对象内的“users”数组中添加一个新用户,并创建一个基本的本地用户数据库

private static void  SignUp() throws IOException, JSONException, ParseException {
        //System.out.println("SignUp");
        String path = "E:\\1PROGRAMMING\\Padlock\\src\\padlock\\register\\info.json";
        String[] labels = {"Age","Name","Second Name","Email","Password"};
        ArrayList<String> dataUser = new ArrayList<>();

      
        Scanner scanner = new Scanner(System.in);

        //check if value Integer
        System.out.println(labels[0]);
        int age = Integer.parseInt(scanner.nextLine());

        if( age >= 18) {
            dataUser.add(Integer.toString(age)); //adding age data to arraylist as first data

            for (int element =1;element< labels.length;element++){ //adding rest of data
                System.out.println(labels[element]);
                String data = scanner.nextLine();
                dataUser.add(data);
            }
            /////////////////////////////////////////////////
            //Spring data request to Python serverless
            /////////////////////////////////////////////////

            System.out.println(dataUser);

            //Add to JSON file
            File exists = new File(path);
            if (exists.exists()) {//check if json exists
                System.out.println("File found.");
                //addToJson()
                addToJson(path, dataUser); //HERE IS THE PROBLEM
            }else{
                System.out.println("File not found... Creating File with the new user");
                //createJson()
                createJson(path, dataUser, labels);
                //createJson(path, name, secondName,age,email,password);
            }
        }else {
            System.out.println("You must be more than 18 years old.");
            System.exit(0);
        }

    }
private static void SignUp()抛出IOException、jsoneexception、ParseException{
//System.out.println(“注册”);
String path=“E:\\1编程\\挂锁\\src\\挂锁\\register\\info.json”;
字符串[]标签={“年龄”、“姓名”、“第二姓名”、“电子邮件”、“密码”};
ArrayList dataUser=新的ArrayList();
扫描仪=新的扫描仪(System.in);
//检查值是否为整数
System.out.println(标签[0]);
int age=Integer.parseInt(scanner.nextLine());
如果(年龄>=18岁){
dataUser.add(Integer.toString(age));//将年龄数据作为第一个数据添加到arraylist
对于(int-element=1;element
还有addToJson函数,我想在其中读取和编辑我的文件

private static void addToJson(String path, ArrayList<String> dataUser) throws IOException, ParseException, JSONException {
        //create jsonobject to add in our path file
        
        //read path file content
        JSONObject ar = new JSONObject(path);

        for (int i = 0; i < ar.length(); i++) {
            System.out.println( "Name: " + ar.getString("Password") );
        }
        
        //Add jsonobject created into our path file

    }
private static void addToJson(字符串路径,ArrayList dataUser)抛出IOException、ParseException、jsoneexception{
//创建要添加到路径文件中的jsonobject
//读取路径文件内容
JSONObject ar=新的JSONObject(路径);
对于(int i=0;i
它将绘制此错误消息:


**线程“main”org.json.JSONException中的异常:JSONObject文本必须以“{”开头,位于E:\1PROGRAMMING\Padlock\src\Padlock\register\info.json**

我想可能是它将我的josn文件作为字符串读取,并找到“[”在错误的索引中,但我需要找到我可以将其作为Json元素读取或直接访问其中的“用户”jsonArray的方法

我认为可能是它将我的josn文件作为字符串读取,并将“[”定位在错误的索引中,但我需要找到我可以将其作为Json元素读取或直接访问“用户”的方法jsonArray在它里面

你的
JSON
是正确的,你可以在

问题就在眼前

JSONObject ar = new JSONObject(path);
由于变量
path
的类型为
String
JSONObject不知道它是否是
.json
文件的路径,因此它试图将
解析为json,并得到一个
JSONParseException
因此会出现错误,请尝试以下操作

String text = new String(Files.readAllBytes(Paths.get(fileName)),StandardCharsets.UTF_8);
JSONObject obj = new JSONObject(text);

您的
JSON
是正确的,您可以在

问题就在眼前

JSONObject ar = new JSONObject(path);
由于变量
path
的类型为
String
JSONObject不知道它是否是
.json
文件的路径,因此它试图将
解析为json,并得到一个
JSONParseException
因此会出现错误,请尝试以下操作

String text = new String(Files.readAllBytes(Paths.get(fileName)),StandardCharsets.UTF_8);
JSONObject obj = new JSONObject(text);
公共类Foo{
公共静态void main(字符串[]args)引发IOException{
Path Path=Path.get(“e:/info.json”);
映射用户=加载用户(路径);
注册(用户);
保存用户(路径,用户);
}
公共静态映射加载用户(路径路径)引发IOException{
JSONObject root=readRootObject(路径);
Map users=newhashmap();
if(root!=null){
对于(对象obj:root.getJSONArray(“用户”)){
JSONObject json=(JSONObject)obj;
用户=新用户();
user.setId(json.getLong(“id”);
user.setEmail(json.getString(“email”);
user.setName(json.getString(“name”);
user.setSecondName(json.getString(“secondName”);
user.setId(json.getInt(“age”);
user.setPassword(json.getString(“password”);
user.put(user.getId(),user);
}
}
返回用户;
}
公共静态void saveUsers(路径、映射用户)引发IOException{
JSONObject root=Optional.ofNullable(readRootObject(path)).orelsGet(JSONObject::new);
root.put(“users”,users.values());
try(Writer-Writer=Files.newBufferedWriter(路径,StandardCharsets.UTF_8)){
writer.write(root.toString(2));
}
}
私有静态JSONObject readRootObject(路径路径)引发IOException{
如果(!Files.isReadable(path))
返回null;
try(Reader=Files.newbufferederader(路径,StandardCharsets.UTF_8)){
返回新的JSONObject(新的JSONTokener(reader));
}
}
公共静态无效注册(地图用户){
尝试(扫描仪扫描=新扫描仪(System.in)){
用户用户=