2011-12-31 44 views
5

我從用戶定義的對象的普通數組創建了一個JSON數組。我如何將JSONArray轉換回用戶定義類型的正常數組。 我使用JSON共享偏好android.Using這個代碼我在網上找到:將JSONArray轉換爲正常數組

import org.json.JSONObject; 
import org.json.JSONArray; 
import org.json.JSONException; 

import android.content.Context; 
import android.content.SharedPreferences; 

public class JSONSharedPreferences { 
    private static final String PREFIX = "json"; 

    public static void saveJSONObject(Context c, String prefName, String key, JSONObject object) { 
     SharedPreferences settings = c.getSharedPreferences(prefName, 0); 
     SharedPreferences.Editor editor = settings.edit(); 
     editor.putString(JSONSharedPreferences.PREFIX+key, object.toString()); 
     editor.commit(); 
    } 

    public static void saveJSONArray(Context c, String prefName, String key, JSONArray array) { 
     SharedPreferences settings = c.getSharedPreferences(prefName, 0); 
     SharedPreferences.Editor editor = settings.edit(); 
     editor.putString(JSONSharedPreferences.PREFIX+key, array.toString()); 
     editor.commit(); 
    } 

    public static JSONObject loadJSONObject(Context c, String prefName, String key) throws JSONException { 
     SharedPreferences settings = c.getSharedPreferences(prefName, 0); 
     return new JSONObject(settings.getString(JSONSharedPreferences.PREFIX+key, "{}")); 
    } 

    public static JSONArray loadJSONArray(Context c, String prefName, String key) throws JSONException { 
     SharedPreferences settings = c.getSharedPreferences(prefName, 0); 
     return new JSONArray(settings.getString(JSONSharedPreferences.PREFIX+key, "[]")); 
    } 

    public static void remove(Context c, String prefName, String key) { 
     SharedPreferences settings = c.getSharedPreferences(prefName, 0); 
     if (settings.contains(JSONSharedPreferences.PREFIX+key)) { 
      SharedPreferences.Editor editor = settings.edit(); 
      editor.remove(JSONSharedPreferences.PREFIX+key); 
      editor.commit(); 
     } 
    } 
} 

我試圖用戶定義的對象陣列轉換成jsonarray並將其存儲在jsonshared偏好和後來試圖回顧它。有問題知道如何回收它。 謝謝。

+1

你正在使用哪個JSon庫?這些細節將有助於快速回答您的問題。 – kosa 2011-12-31 17:37:38

+0

編輯詳情。 – shady2020 2011-12-31 18:21:31

回答

0

如果您使用的是Android自帶的JSONObject,那麼將其從用戶定義的類型轉換爲JSONObject/JSONArray然後再返回是很乏味的。還有其他一些庫可以自動完成這種轉換,所以對於解碼/編碼JSON很簡單。

ProductLineItem lineItem = ...; 
JSONObject json = new JSONObject(); 
json.put("name", lineItem.getName()); 
json.put("quantity", lineItem.getCount()); 
json.put("price", lineItem.getPrice()); 
... // do this for each property in your user defined class 
String jsonStr = json.toString(); 

這可以全部封裝在ProductLineItem.toJSON()中。解析是類似的。我想創建一個構造函數一個JSONObject並創建對象,如:ProductLineItem OBJ =新ProductLineItem(的JSONObject):

public class ProductLineItem { 
    private String name; 
    private int quantity; 
    private float price; 

    public MyObject(JSONObject json) { 
     name = json.getString("name"); 
     count = json.getInt("quantity"); 
     price = json.optFloat("price"); 
    } 
} 

處理陣列是大同小異。所以像這樣:

public class ShoppingCart { 

    float totalPrice; 
    List<Rebate> rebates = new ArrayList<Rebate>(); 
    List<ProductLineItem> lineItems = new ArrayList<ProductLineItem>(); 


    public ShoppingCart(JSONObject json) { 
     totalPrice = json.getFloat("totalPrice"); 

     for(JSONObject rebateJson : json.getArray("rebates")) { 
      rebates.add(new Rebate(rebateJson)); 
     } 

     for(JSONObject productJson : json.getArray("lineItems")) { 
      lineItems.add(new ProductLineItem(productJson)); 
     } 
    } 

    public JSONObject toJSON() { 
     JSONObject json = new JSONObject(); 
     json.put("totalPrice", totalPrice); 

     JSONArray rebatesArray = new JSONArray(); 
     for(Rebate rebate : rebates) { 
      rebatesArray.put(rebate.toJSON()); 
     } 

     JSONArray lineItemsArray = new JSONArray(); 
     for(ProductLineItem lineItem : lineItems) { 
      lineItemsArray.put(lineItem.toJSON()); 
     } 

     json.put("rebates", rebatesArray); 
     json.put("lineItems", lineItemsArray); 

     return json; 
    } 
} 

你可以看到一個簡單的2個對象,這個鍋爐代碼是相當重要的。所以,你可以繼續這樣做,或者你可以使用一個處理所有這一切都爲你的庫文件:

http://flexjson.sourceforge.net

// serialize 
String json = new JSONSerializer().serialize(shoppingCart); 
// deserialize 
ShoppingCart cart = new JSONDeserializer<ShoppingCart>().deserialize(json, ShoppingCart.class); 
+0

這回答我的問題是最好的。雖然我只是使用普通的共享偏好爲我的目的。我想實現一個簡單的高分系統。我想這可以完成將分數和詳細信息存儲在單個字符串共享偏好,然後在回覆期間對其進行解析。 – shady2020 2012-01-01 08:46:11

+0

您當然可以將json字符串寫入共享首選項,就像您希望使用上述方法一樣。使用JSON庫與直接寫入共享首選項的代碼量基本相同。它仍然是大致相同的翻譯代碼,用於將對象從模型轉換爲JSON或共享首選項。 – chubbsondubs 2012-01-01 15:42:12

0

我假設你的JSON數組包含相同基元類型(字符串,整型,浮點數等 - 如果不是那麼你將有問題)的多個實例。

在這種情況下,你需要做這樣的事情(假設字符串數組):

String[] strArray = new String[jsonArray.length()]; 

for (int i = 0; i < jsonArray.length(); i++) { 
    strArray[i] = jsonArray.getString(i); 
} 

顯然,如果你是在處理與其他基本類型進行適當的替換。