Android 将JSONArray转换为普通数组
我从用户定义对象的普通数组创建了一个JSON数组。如何将JSONArray转换回用户定义类型的普通数组。。? 我在android中使用Json作为共享首选项。使用我在网上找到的代码:Android 将JSONArray转换为普通数组,android,json,sharedpreferences,arrays,Android,Json,Sharedpreferences,Arrays,我从用户定义对象的普通数组创建了一个JSON数组。如何将JSONArray转换回用户定义类型的普通数组。。? 我在android中使用Json作为共享首选项。使用我在网上找到的代码: import org.json.JSONObject; import org.json.JSONArray; import org.json.JSONException; import android.content.Context; import android.content.SharedPreference
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首选项中,然后尝试检索它。在知道如何检索它时遇到问题。
谢谢。如果您使用的是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=new ProductLineItem(JSONObject):
处理数组是非常相似的。比如:
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;
}
}
公共类购物车{
浮动总价格;
列表折扣=新的ArrayList();
List lineItems=new ArrayList();
公共购物车(JSONObject json){
totalPrice=json.getFloat(“totalPrice”);
for(JSONObject rebateJson:json.getArray(“rebates”)){
折扣。添加(新折扣(rebateJson));
}
对于(JSONObject productJson:json.getArray(“lineItems”)){
添加(新的ProductLineItem(productJson));
}
}
公共JSONObject toJSON(){
JSONObject json=新的JSONObject();
json.put(“totalPrice”,totalPrice);
JSONArray rebatesArray=新的JSONArray();
用于(返利:返利){
rebatesArray.put(rebait.toJSON());
}
JSONArray lineItemsArray=新的JSONArray();
对于(ProductLineItem lineItem:lineItems){
lineItemsArray.put(lineItem.toJSON());
}
put(“rebates”,rebatesArray);
put(“lineItems”,lineItemsArray);
返回json;
}
}
您可以看到,对于一个简单的2个对象,这个锅炉板代码非常重要。因此,您可以继续这样做,也可以使用一个库为您处理所有这些:
//序列化
String json=new JSONSerializer().serialize(shoppingCart);
//反序列化
ShoppingCart cart=new-JSONDeserializer()。反序列化(json,ShoppingCart.class);
我过去曾使用谷歌的gson库来实现这一点。请看这里:
我假设您的JSON数组包含相同基元类型的多个实例(字符串、int、float等-如果不是,那么您将遇到问题) 在这种情况下,您需要执行以下操作(假设字符串数组):
String[]strArray=新字符串[jsonArray.length()];
for(int i=0;i
显然,如果您正在处理其他原语类型,请进行适当的替换。您使用的是哪个JSon库?这些详细信息将有助于快速回答您的问题。这是对我问题的最好回答。虽然我只使用了普通共享首选项,但我想实现一个简单的高分系统。我想这可以通过将分数和详细信息存储在共享首选项中的单个字符串中,然后进行解析来完成您当然可以使用上面的方法将json字符串写入共享首选项,就像您希望的那样。使用JSON库与直接写入共享pref的代码量基本相同。将对象从模型转换为JSON或共享pref仍然是大致相同的转换代码。
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;
}
}
// serialize
String json = new JSONSerializer().serialize( shoppingCart );
// deserialize
ShoppingCart cart = new JSONDeserializer<ShoppingCart>().deserialize( json, ShoppingCart.class );
String[] strArray = new String[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
strArray[i] = jsonArray.getString(i);
}