/
Letta21
/
HWAbstrInheritance
Обзор
Документация
Войти
/
Letta21
/
HWAbstrInheritance
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
JSONwork
82 строки
4 KB
Letta21
update JSONwork
15 янв 2026, 01:11
15 янв 2026, 01:11
0ee15a5
Код
Авторство
О чём код?
import com.google.gson.Gson; import com.google.gson.GsonBuilder; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; import java.awt.*; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class mainJson { public static void main(String[] args) { JSONParser parser = new JSONParser(); JSONObject jsonObject = null; try { Object obj = parser.parse(new FileReader("data.json")); jsonObject = (JSONObject) obj; System.out.println(jsonObject); } catch (IOException | ParseException e) { e.printStackTrace(); } String lastName = (String) jsonObject.get("lastName"); System.out.println(lastName); JSONObject adress = (JSONObject) jsonObject.get("address"); String city = (String) adress.get("streetAddress"); System.out.println(city); JSONArray phoneNumbers = (JSONArray) jsonObject.get("phoneNumbers"); for (Object number : phoneNumbers) { System.out.println(number); } JSONObject obj = new JSONObject(); obj.put("name", "mkyong.com"); obj.put("age", 100); JSONArray list = new JSONArray(); list.add("msg 1"); list.add("msg 2"); list.add("msg 3"); obj.put("messages", list); try (FileWriter file = new FileWriter("new_data.json")) { file.write(obj.toJSONString()); file.flush(); } catch (IOException e) { e.printStackTrace(); } Cat cat = new Cat(); cat.name = "Матроскин"; cat.age = 5; cat.color = Color.blue.getRGB(); //Конвертируем объект созданного класса в JSON при помощи метода toJson(): GsonBuilder builder = new GsonBuilder(); Gson gson = builder.create(); System.out.println(gson.toJson(cat)); //Конвертируем JSON в объект String jsonText = "{\"name\":\"Мурзик\",\"color\":-16777216,\"age\":9}"; builder = new GsonBuilder(); gson = builder.create(); cat = gson.fromJson(jsonText, Cat.class); System.out.println("Имя: " + cat.name + "\nВозраст: " + cat.age + "\nЦвет: " + cat.color); } } C:\Users\Letta\.jdks\openjdk-24.0.2+12-54\bin\java.exe "-javaagent:C:\Users\Letta\AppData\Local\Programs\IntelliJ IDEA Community Edition\lib\idea_rt.jar=61230" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath C:\Users\Letta\IdeaProjects\json\target\classes;C:\Users\Letta\.m2\repository\com\googlecode\json-simple\json-simple\1.1.1\json-simple-1.1.1.jar;C:\Users\Letta\.m2\repository\junit\junit\4.10\junit-4.10.jar;C:\Users\Letta\.m2\repository\org\hamcrest\hamcrest-core\1.1\hamcrest-core-1.1.jar;C:\Users\Letta\.m2\repository\com\google\code\gson\gson\2.8.5\gson-2.8.5.jar mainJson {"firstName":"Иван","lastName":"Иванов","address":{"streetAddress":"Московское \r\nш., 101, кв.101","city":"Ленинград","postalCode":101101},"phoneNumbers":["812 123-1234","916 123-4567"]} Иванов Московское ш., 101, кв.101 812 123-1234 916 123-4567 {"name":"Матроскин","age":5,"color":-16776961} Имя: Мурзик Возраст: 9 Цвет: -16777216 Process finished with exit code 0