2011-12-31 126 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); 
} 

显然,如果你是在处理与其他基本类型进行适当的替换。