我正在使用 net.sf.json 包來處理 json。我有一個名為 CustomerClass 的類。我想在一個JSON物件中存盤這個類的實體,并在以后檢索這個類的實體。
我的代碼。
import net.sf.json.JSONObject;
class Test {
JSONObject test = new JSONObject;
CustomerClass obj = new CustomerClass() 。
test.put("objInstance", (Object) obj);
CustomerClass retrievedInstance = (CustomerClass) test.get("objInstance"); // throws the errorclass net.sf.json.JSONObject不能被轉換為class。
com.CustomerClass(net.sf.json.JSONObject和
com.CustomerClass在加載器的未命名module中。
org.apache.catalina.loader.ParallelWebappClassLoader @18245eb0)
uj5u.com熱心網友回復:
根據JSONObject的javadocs 這里,
值可以是這些型別中的任何一種。布林值、JSONArray、JSONObject、數字、字串或JSONNull物件
所以基本上你不能把任何物件放到JSONObject中,然后再去檢索它。如果我嘗試的話,它會把
{}.public class CustomerClass { public CustomerClass(String string, String string2) { name = string; id=string2; } 字串名稱。 String id; } JSONObject test = new JSONObject (); CustomerClass obj = new CustomerClass("Hello", "Hi")。) String testStr = "hi"; test.put("objInstance", (Object)obj) 。 test.put("str", testStr); Object retrievedInstance = test.get("objInstance"); //拋出錯誤。 String retrivedStr = (String)test.get("str") 。 System.out.println(retrivedStr)。 System.out.println(retrievedInstance); System.out.println(test)。輸出:
Hi {} {"objInstance":{},"str":"Hi"}。因此,出路是你可以使用GSON庫將你的Class CustomerClass映射到JSONObject,然后再檢索它。閱讀這個.
。轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/319409.html
標籤:
上一篇:在畫布中對多個圓圈進行影片處理
下一篇:Tomcat上的Spring應用程式-背景關系路徑/web-customer-rest-demo的應用程式無法啟動
