在这段代码中,我解析了JSON并使用ArrayList
在可搜索的微调器中显示它。代码完美无缺,但是当解析大型JSON时,移动数据需要时间和内存。
任何人都可以建议我更好地解析大型JSON,或者任何人都可以解释我如何用Gson解析它并加载到可搜索的微调器中?
try {
if (response.has("products_data")) {
jsonArray = new JSONArray();
jsonArray = response.getJSONArray("products_data");
for (int t = 0; t < jsonArray.length(); t++) {
object = jsonArray.getJSONObject(t);
retailer_id = object.getString("retailer_id");
retailer_name = object.getString("retailer_name");
product_code = object.getString("product_code");
product_name = object.getString("product_name");
pro_packing = object.getString("pro_packing");
pro_company=object.getString("pro_company");
pro_generic_code=object.getString("pro_generic_code");
pro_generic_name=object.getString("pro_generic_name");
pro_stock = object.getInt("product_stock");
product_MRP=object.getDouble("product_MRP");
p = new Product();
p.setRetailer_id(retailer_id);
p.setRetailer_name(retailer_name);
p.setProduct_code(product_code);
p.setProduct_name(product_name);
p.setPro_packing(pro_packing);
p.setPro_company(pro_company);
p.setPro_generic_code(pro_generic_code);
p.setPro_generic_name(pro_generic_name);
p.setPro_stock(pro_stock);
p.setProduct_MRP(product_MRP);
productlist.add(p);
}
progressDialog.dismiss();
final List<String> pname = new ArrayList<String>();
final List<String> pcode = new ArrayList<String>();
pname.clear();
pname.add(0,"Select Products");
for (int i = 0; i < productlist.size(); i++) {
pname.add(productlist.get(i).getProduct_name());
}
// Creating adapter for spinner
final ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.custom_spinnertxt, pname);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
runOnUiThread(new Runnable() {
@Override
public void run() {
// attaching data adapter to spinner
autoCompleteTextView.setAdapter(spinnerAdapter);
autoCompleteTextView.setSelection(0);
}
});
autoCompleteTextView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int i, long id) {
if(i>0) {
p_id = productlist.get(i - 1).getProduct_code();
p_stock = productlist.get(i - 1).getPro_stock();
ppack = productlist.get(i - 1).getPro_packing();
pcomp = productlist.get(i - 1).getPro_company();
mrp = productlist.get(i - 1).getProduct_MRP();
pseller = productlist.get(i - 1).getRetailer_name();
// Log.i("pID:", p_id);
//Log.i("pStock:", String.valueOf(p_stock));
//Log.i("PMRP:", String.valueOf(mrp));
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
这是我的JSON响应:
"products_data": [
{
"retailer_id": "mum0022",
"retailer_name": "SAKTI MEDICO",
"product_code": "6474",
"product_name": "EPISOFT CLEANSING LOTION",
"pro_packing": "125ML",
"pro_company": "GLENMARK-GRACEWELLSPECIALITY",
"pro_generic_code": "",
"pro_generic_name": "",
"product_stock": "7",
"product_MRP": "209"
},
{
"retailer_id": "mum0022",
"retailer_name": "SAKTI MEDICO",
"product_code": "8403",
"product_name": "ELOVERA LOTION 150ML (BIG)",
"pro_packing": "150ML",
"pro_company": "GLENMARK (DERMAX)",
"pro_generic_code": "",
"pro_generic_name": "",
"product_stock": "3",
"product_MRP": "324.5"
},
...
答案 0 :(得分:1)
试试这个
public class Products_data implements Parcelable {
@SerializedName("retailer_id")
private String retailer_id;
@SerializedName("retailer_name")
private String retailer_name;
@SerializedName("product_code")
private String product_code;
@SerializedName("product_name")
private String product_name;
@SerializedName("pro_packing")
private String pro_packing;
@SerializedName("pro_company")
private String pro_company;
@SerializedName("pro_generic_code")
private String pro_generic_code;
@SerializedName("pro_generic_name")
private String pro_generic_name;
@SerializedName("product_stock")
private Integer product_stock;
@SerializedName("product_MRP")
private Double product_MRP;
public Products_data() {
}
protected Products_data(Parcel in) {
retailer_id = in.readString();
retailer_name = in.readString();
product_code = in.readString();
product_name = in.readString();
pro_packing = in.readString();
pro_company = in.readString();
pro_generic_code = in.readString();
pro_generic_name = in.readString();
product_stock = in.readInt();
product_MRP = in.readDouble();
}
public static final Creator<Products_data> CREATOR = new Creator<Products_data>() {
@Override
public Products_data createFromParcel(Parcel in) {
return new Products_data(in);
}
@Override
public Products_data[] newArray(int size) {
return new Products_data[size];
}
};
public String getRetailer_id() {
return retailer_id;
}
public void setRetailer_id(String retailer_id) {
this.retailer_id = retailer_id;
}
public String getRetailer_name() {
return retailer_name;
}
public void setRetailer_name(String retailer_name) {
this.retailer_name = retailer_name;
}
public String getProduct_code() {
return product_code;
}
public void setProduct_code(String product_code) {
this.product_code = product_code;
}
public String getProduct_name() {
return product_name;
}
public void setProduct_name(String product_name) {
this.product_name = product_name;
}
public String getPro_packing() {
return pro_packing;
}
public void setPro_packing(String pro_packing) {
this.pro_packing = pro_packing;
}
public String getPro_company() {
return pro_company;
}
public void setPro_company(String pro_company) {
this.pro_company = pro_company;
}
public String getPro_generic_code() {
return pro_generic_code;
}
public void setPro_generic_code(String pro_generic_code) {
this.pro_generic_code = pro_generic_code;
}
public String getPro_generic_name() {
return pro_generic_name;
}
public void setPro_generic_name(String pro_generic_name) {
this.pro_generic_name = pro_generic_name;
}
public Integer getProduct_stock() {
return product_stock;
}
public void setProduct_stock(Integer product_stock) {
this.product_stock = product_stock;
}
public Double getProduct_MRP() {
return product_MRP;
}
public void setProduct_MRP(Double product_MRP) {
this.product_MRP = product_MRP;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(retailer_id);
parcel.writeString(retailer_name);
parcel.writeString(product_code);
parcel.writeString(product_name);
parcel.writeString(pro_packing);
parcel.writeString(pro_company);
parcel.writeString(pro_generic_code);
parcel.writeString(pro_generic_name);
parcel.writeInt(product_stock);
parcel.writeDouble(product_MRP);
}
}
制作此POJO课程后
gson.fromJson(json, Products_data.class);