org.json - Propriété

La classe Property fournit des méthodes statiques pour convertir le texte des propriétés en JSONObject, et vice versa.

Les méthodes suivantes sont couvertes dans l'exemple.

  • toJSONObject(Properties) - Convertit les données de propriétés en objet JSONObject.

  • toProperties(JSONObject) - Convertit un JSONObject en objet de propriétés.

Exemple

import java.util.Properties;
import org.json.JSONObject;
import org.json.Property;

public class JSONDemo {
   public static void main(String[] args) {
      Properties properties = new Properties();
      properties.put("title", "This is a title text");
      properties.put("subtitle", "This is a subtitle text");

      System.out.println("Properties to JSON");
      JSONObject jsonObject = Property.toJSONObject(properties);
      System.out.println(jsonObject);

      System.out.println("JSON to properties");
      System.out.println(Property.toProperties(jsonObject));
   }
}

Production

Properties to JSON
{"subtitle":"This is a subtitle text","title":"This is a title text"}
JSON to properties
{subtitle = This is a subtitle text, title = This is a title text}