JSON Java的`put()`方法返回String,而不是void?`put()`方法不起作用!!;((

huangapple go评论70阅读模式
英文:

JSON Java put() return String, not void? put() not working!! ;((

问题

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

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

class Edit_Details_OnlyAdmin_Loop {

    public void Edit_Details_OnlyAdmin_Loop_main(int Option, String newVal, String credUserName, String credPwd) {
        try {
            JSONParser parser = new JSONParser();

            JSONArray a = (JSONArray) parser.parse(new FileReader(Main.databasepath));

            switch (Option) {
                case 1:
                    for (Object o : a) {
                        JSONObject User = (JSONObject) o;
                        String Username = (String) User.get("Username");
                        String Pwd = (String) User.get("Password");

                        if (credUserName.equals(Username) && credPwd.equals(Pwd)) {
                            User.put("Name", newVal);
                        }
                    }
                    break;
            }

            // Write the updated JSONArray back to the file
            // (This part is missing in your original code)
            // You'll need to import the necessary classes for file writing

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }
}

在上述代码中,在case 1的情况下,代码将遍历JSON数组,找到与给定用户名和密码匹配的用户对象,然后使用put方法将键为"Name"的值替换为新的输入数据。请注意,您的原始代码中没有将更新后的JSONArray写回文件的部分,您需要根据需要添加这部分代码。

英文:
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
class Edit_Details_OnlyAdmin_Loop {
public void Edit_Details_OnlyAdmin_Loop_main(int Option, String newVal, String credUserName, String credPwd) {
try {
JSONParser parser = new JSONParser();
JSONArray a = (JSONArray) parser.parse(new FileReader(Main.databasepath));
// System.out.println("a is"+a);
switch (Option) {
case 1:
for (Object o : a) {
// System.out.println("o is"+o);
JSONObject User = (JSONObject) o;
// System.out.println("user is:" + User);
String Username = (String) User.get("Username");
String Pwd = (String) User.get("Password");
// JSONObject new1= (JSONObject) User.get("Name");
if (credUserName.equals(Username) && credPwd.equals(Pwd)) {
// System.out.println(User.remove("Name"));
User.put("Name", newVal);
// System.out.println("updated successfully");
}
}
break;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
}

above code, inside the for loop, the json .put() i had tried to system print the .put and return a value instead of a void function...
i just want to replace my value inside the json file using the .put()...
all the parameter passed is correct, been checked one by one

below is my database file

[{"Role":"1","AssesType":"","Username":"","Module":"","Marks":"","Name":"q","Password":"123","Programme":""},{"Role":"3","AssesType":"1","Username":"tp054660","Module":"2","Marks":"","Name":"yaphanyee","Password":"123","Programme":"1"},{"Role":"3","AssesType":"2","Username":"tp054660","Module":"2","Marks":"","Name":"yaphanyee","Password":"123","Programme":"1"},{"Role":"1","AssesType":"","Username":"jsn","Module":"","Marks":"","Name":"jason","Password":"123","Programme":""},{"Role":"1","AssesType":"","Username":"123","Module":"","Marks":"","Name":"tp054660","Password":"123","Programme":""}]

above code demonstrate the effort of changing the value stored as key 'Name' and replace with new input data....

and the put() is not working as expected, too there is no any catch error

答案1

得分: 1

你需要将修改后的 JSON 数据写入磁盘,如果想在文件中看到任何实际变更。

使用类似以下代码的方式(关于您的一些数据类型我不是完全确定,所以可能会有轻微的编译错误):

try (FileWriter file = new FileWriter(Main.databasepath)) {
    file.write(a.toJSONString());
    file.flush();
} catch (IOException e) {
    e.printStackTrace();
}
英文:

You need to write your changed JSON data to disk if you want to see any actual changes in your file.

Use a code similar to the following (I'm not completely sure about some of your datatypes, so there might be slight compile errors):

try (FileWriter file = new FileWriter(Main.databasepath)) {
file.write(a.toJSONString());
file.flush();
} catch (IOException e) {
e.printStackTrace();
}

huangapple
  • 本文由 发表于 2020年8月28日 18:14:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63631820.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定