在 JSON 数组中创建一个 JSON 对象(JSON-简单/JAVA)
Create a JSONObject in a JSONArray (JSON-Simple / JAVA)
所以,我在 JSON 数组中添加新的 JSON 对象时遇到了很多麻烦。
我的代码是西班牙语的,所以我会尽量在我的评论中将变量和方法翻译成英语。
这是我的.JSON(默认用户拥有"admin"的所有属性):
{"Usuarios":[{"nombre":"Admin","apellido":"Admin","direccion":"Admin","telefono":"Admin","correo":"Admin@admin.com","username":"admin","password":"admin"}]}
首先,我创建了函数,可以毫无问题地验证我的用户 "admin" 的属性。所以,他们在读取 JSON 文件时没有问题,只是在上面写:
问题来了,这个函数:
public void agregarUsuario(String nombre, String apellido, String direccion, String telefono, String correo, String username, String password) //this function add a new user object to my array in the json
{
try {
JSONObject usuarios = getJSONObjectFromFile("/usuarios.json"); //Json object "usuarios"/"users" is my main json obj file
JSONArray listaUsuario = usuarios.getJSONArray("Usuarios"); //I've start my array "Usuarios" (who is in my .json)
JSONObject newObject = new JSONObject(); //I created a new JSONObjectVariable, with the attributes i want in it
newObject.put("nombre", nombre);
newObject.put("apellido", apellido);
newObject.put("direccion", direccion);
newObject.put("telefono", telefono);
newObject.put("correo", correo);
newObject.put("username",username);
newObject.put("password", password);
//at this point my new object has all the attributes i want to put in my array
listaUsuario.put(newObject); //so here i tried to put in my array my new Json object
usuarios.put("Usuarios",listaUsuario); //this line, is one of my attempts to figure out how to put the function working
//it wasn't in the first time, but how listaUsuario.put(newObject) dint make any change in my .json...
//with or without it doesn't do anything
}catch(JSONException e) {
e.printStackTrace();
}
}
这个 "writes" 的函数没有对我的 json 文件“/usuarios.json”进行任何更改,但是,我实际上可以毫无问题地阅读我的 json;例如,我的项目的这个其他功能读取我的 json 文件以查看密码和用户名是否与登录 JFrame 匹配:
public boolean verificarInicioDeUsuario(String incomeUsername,String incomePassword) {
JSONArray listaUsuario = usuarios.getJSONArray("Usuarios"); //gets in my "Usuarios"/"User" array
for(int j=0;j<listaUsuario.length();j++) { //Start reading my array
JSONObject user = listaUsuario.getJSONObject(j); //i make a new JSONObject for use it to compare my strings
if((user.getString("username").equals(incomeUsername))&&(user.getString("password").equals(incomePassword))) { //I compare my object username attributes with my incomeStrings
return true; //if the username and password matches in the same object, returns true
}
}
return false; //if not, returns false
}
有suggestion/help吗? <3
你只是把加载的JSON对象改成了内存。您尚未将其写入文件。请使用下面的代码示例并根据您的程序进行更改。
所以这里的主要问题是:
在 JSONObject 上调用 toString,然后序列化该字符串。 JSON对象本身不可序列化。所以你必须在下面使用:
String jsonString = jsonObject.toString();
示例代码:
public void agregarUsuario(String nombre, String apellido, String direccion, String telefono, String correo, String username, String password) //this function add a new user object to my array in the json
{
try {
JSONObject usuarios = getJSONObjectFromFile("/usuarios.json"); //Json object "usuarios"/"users" is my main json obj file
JSONArray listaUsuario = usuarios.getJSONArray("Usuarios"); //I've start my array "Usuarios" (who is in my .json)
JSONObject newObject = new JSONObject(); //I created a new JSONObjectVariable, with the attributes i want in it
newObject.put("nombre", nombre);
newObject.put("apellido", apellido);
newObject.put("direccion", direccion);
newObject.put("telefono", telefono);
newObject.put("correo", correo);
newObject.put("username",username);
newObject.put("password", password);
//at this point my new object has all the attributes i want to put in my array
listaUsuario.put(newObject); //so here i tried to put in my array my new Json object
usuarios.put("Usuarios",listaUsuario); //this line, is one of my attempts to figure out how to put the function working
//it wasn't in the first time, but how listaUsuario.put(newObject) dint make any change in my .json...
//with or without it doesn't do anything
ObjectOutputStream outputStream = null;
outputStream = new ObjectOutputStream(new FileOutputStream("/usuarios.json"));
System.out.println("Start Writing in to file ");
outputStream.writeObject(usuarios.toString());
outputStream.flush();
outputStream.close();
}catch(JSONException e) {
e.printStackTrace();
}
catch (Exception e){
System.err.println("Error writting json: " + e);
}
}
所以,我在 JSON 数组中添加新的 JSON 对象时遇到了很多麻烦。
我的代码是西班牙语的,所以我会尽量在我的评论中将变量和方法翻译成英语。
这是我的.JSON(默认用户拥有"admin"的所有属性):
{"Usuarios":[{"nombre":"Admin","apellido":"Admin","direccion":"Admin","telefono":"Admin","correo":"Admin@admin.com","username":"admin","password":"admin"}]}
首先,我创建了函数,可以毫无问题地验证我的用户 "admin" 的属性。所以,他们在读取 JSON 文件时没有问题,只是在上面写:
问题来了,这个函数:
public void agregarUsuario(String nombre, String apellido, String direccion, String telefono, String correo, String username, String password) //this function add a new user object to my array in the json
{
try {
JSONObject usuarios = getJSONObjectFromFile("/usuarios.json"); //Json object "usuarios"/"users" is my main json obj file
JSONArray listaUsuario = usuarios.getJSONArray("Usuarios"); //I've start my array "Usuarios" (who is in my .json)
JSONObject newObject = new JSONObject(); //I created a new JSONObjectVariable, with the attributes i want in it
newObject.put("nombre", nombre);
newObject.put("apellido", apellido);
newObject.put("direccion", direccion);
newObject.put("telefono", telefono);
newObject.put("correo", correo);
newObject.put("username",username);
newObject.put("password", password);
//at this point my new object has all the attributes i want to put in my array
listaUsuario.put(newObject); //so here i tried to put in my array my new Json object
usuarios.put("Usuarios",listaUsuario); //this line, is one of my attempts to figure out how to put the function working
//it wasn't in the first time, but how listaUsuario.put(newObject) dint make any change in my .json...
//with or without it doesn't do anything
}catch(JSONException e) {
e.printStackTrace();
}
}
这个 "writes" 的函数没有对我的 json 文件“/usuarios.json”进行任何更改,但是,我实际上可以毫无问题地阅读我的 json;例如,我的项目的这个其他功能读取我的 json 文件以查看密码和用户名是否与登录 JFrame 匹配:
public boolean verificarInicioDeUsuario(String incomeUsername,String incomePassword) {
JSONArray listaUsuario = usuarios.getJSONArray("Usuarios"); //gets in my "Usuarios"/"User" array
for(int j=0;j<listaUsuario.length();j++) { //Start reading my array
JSONObject user = listaUsuario.getJSONObject(j); //i make a new JSONObject for use it to compare my strings
if((user.getString("username").equals(incomeUsername))&&(user.getString("password").equals(incomePassword))) { //I compare my object username attributes with my incomeStrings
return true; //if the username and password matches in the same object, returns true
}
}
return false; //if not, returns false
}
有suggestion/help吗? <3
你只是把加载的JSON对象改成了内存。您尚未将其写入文件。请使用下面的代码示例并根据您的程序进行更改。
所以这里的主要问题是: 在 JSONObject 上调用 toString,然后序列化该字符串。 JSON对象本身不可序列化。所以你必须在下面使用:
String jsonString = jsonObject.toString();
示例代码:
public void agregarUsuario(String nombre, String apellido, String direccion, String telefono, String correo, String username, String password) //this function add a new user object to my array in the json
{
try {
JSONObject usuarios = getJSONObjectFromFile("/usuarios.json"); //Json object "usuarios"/"users" is my main json obj file
JSONArray listaUsuario = usuarios.getJSONArray("Usuarios"); //I've start my array "Usuarios" (who is in my .json)
JSONObject newObject = new JSONObject(); //I created a new JSONObjectVariable, with the attributes i want in it
newObject.put("nombre", nombre);
newObject.put("apellido", apellido);
newObject.put("direccion", direccion);
newObject.put("telefono", telefono);
newObject.put("correo", correo);
newObject.put("username",username);
newObject.put("password", password);
//at this point my new object has all the attributes i want to put in my array
listaUsuario.put(newObject); //so here i tried to put in my array my new Json object
usuarios.put("Usuarios",listaUsuario); //this line, is one of my attempts to figure out how to put the function working
//it wasn't in the first time, but how listaUsuario.put(newObject) dint make any change in my .json...
//with or without it doesn't do anything
ObjectOutputStream outputStream = null;
outputStream = new ObjectOutputStream(new FileOutputStream("/usuarios.json"));
System.out.println("Start Writing in to file ");
outputStream.writeObject(usuarios.toString());
outputStream.flush();
outputStream.close();
}catch(JSONException e) {
e.printStackTrace();
}
catch (Exception e){
System.err.println("Error writting json: " + e);
}
}