Answer the question
In order to leave comments, you need to log in
How to add values to json array?
How can i write new data to json array? I have a json file, it has an array of Workers, which consists of the name, age, pay, post keys and their values. How can I write 4 more of the same keys to this array? I am using the Simple Json library
{
"Workers": [
{
"name": "Имя1",
"post": "Должнось1",
"age": 24,
"pay": 20000
},
{
"name": "Имя2",
"post": "Должнось2",
"age": 25,
"pay": 16000
}
//Добавить вот так
// {
// "name": "Имя3"
// "post": "Должность3"
// "age": "27"
// "pay": "15000"
// },
]
}
FileReader reader = new FileReader(filePath);
JSONParser jsonParser = new JSONParser();
JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);
JSONArray lang= (JSONArray) jsonObject.get("Workers");
Iterator i = lang.iterator();
while (i.hasNext()) {
JSONObject innerObj = (JSONObject) i.next();
System.out.println("ФИО: " + innerObj.get("name") + "\n"
+ "Должность: " + innerObj.get("post") + "\n"
+ "Возраст: " + innerObj.get("age") + "\n"
+ "Зарплата: " + innerObj.get("pay") + "\n");
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question