📜  org.json-属性

📅  最后修改于: 2020-11-12 05:09:40             🧑  作者: Mango


属性类提供了将属性文本转换为JSONObject的静态方法,反之亦然。

示例中涵盖了以下方法。

  • toJSONObject(Properties) -将属性数据转换为JSONObject对象。

  • toProperties(JSONObject) -将JSONObject转换为属性对象。

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));
   }
}

输出

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}