Java中org.simple.json和org.json库之间的区别?

org.json.simple库使我们能够阅读和Java编写JSON数据。换句话说,我们可以编码 解码 JSON对象。该org.json.simple包中包含重要的类象 JSONValue,的JSONObject,JSONArray,JsonStringJsonNumber。我们需要安装 json-simple.jar 文件来执行JSON程序,而org.json库具有用于解析Java JSON的类。它还在JSON XML,HTTP标头,Cookie CDF之间转换。该org.json包中包含重要的类象 JSONObject,JSONTokener,JSONWriter,JSONArray,CDL,CookieCookieList。我们需要安装json.jar文件以执行JSON程序。

org.simple.json包示例

import org.json.simple.JSONObject;
public class SimpleJsonTest {
   public static void main(String[] args) {
      JSONObject jsonObj = new JSONObject();
      jsonObj.put("empName", "Raja");
      jsonObj.put("employeeId", "115");
      jsonObj.put("age","30");
      System.out.println(jsonObj.toJSONString());
   }
}

输出结果

{"empName":"Raja","employeeId":"115","age":"30"}


org.json包示例

import org.json.*;
public class JSONTest {
   public static void main(String args[]) throws JSONException {
      String json = "{" + "Name : Jai," + "Age : 25, " + "Salary: 25000.00 " + "}";
      JSONObject jsonObj = new JSONObject(json);
      System.out.println(jsonObj.toString());
   }
}

输出结果

{"Salary":25000,"Age":25,"Name":"Jai"}