Java __exclusive__ — Json Library

JSONArray hobbies = new JSONArray(); hobbies.put("reading"); hobbies.put("swimming"); obj.put("hobbies", hobbies);

System.out.println(obj.toString(2)); // pretty print with 2 spaces indent

import org.json.JSONObject; import org.json.JSONArray; JSONObject obj = new JSONObject(); obj.put("name", "David"); obj.put("age", 40); json library java

import com.google.gson.Gson; Gson gson = new Gson(); User user = new User("Charlie", 35); String json = gson.toJson(user);

Whichever library you choose, mastering JSON processing is essential for modern Java development. Now go convert those objects to JSON and back! Did I miss your favorite library? Let me know about JSON-smart, Moshi (from Square), or Boon in the comments below! JSONArray hobbies = new JSONArray(); hobbies

// Deserialize User result = jsonb.fromJson(json, User.class); | Library | Serialization Speed | Deserialization Speed | Memory Usage | |---------|--------------------|----------------------|---------------| | Jackson | Fastest | Fastest | Moderate | | Gson | Fast | Fast | Low | | JSON-java | Slow | Slow | High (creates many objects) | | JSON-B (Yasson) | Moderate | Moderate | Moderate |

String json = "\"name\":\"Bob\",\"age\":25"; User user = mapper.readValue(json, User.class); System.out.println(user.getName()); // Bob Let me know about JSON-smart, Moshi (from Square),

User user = gson.fromJson(json, User.class);

You are at:
Motorola