请帮助解析 json 以使用 volley 建模

please help parse json to model using volley

我有json这样输出

{
    "message": "success",
    "battery": "AHAJAJ1DH13T0021",
    "data": {
        "id": 6,
        "userId": 3,
        "shopId": 1,
        "transactionStatus": "PENDING",
        "expiredAt": "2019-01-04T03:01:18.878Z",
        "updatedAt": "2019-01-04T02:01:18.916Z",
        "createdAt": "2019-01-04T02:01:18.916Z",
        "paymentId": null,
        "batteryNo": null
    }
}

这是我的模特class

public class BookBattery implements Serializable {

private int id;
private String ebikeSn;
private String bookTime;
private String batterySn;
private String transactionStatus;
private String paymentId;
private String batteryNo;
private String addressBook;
private int leftSeconds;

这是我的适配器

public class BookBatteryListAdapter extends RecyclerView.Adapter {

private Context mContext;
private List<BookBattery> mBookBatteryList = new ArrayList<>();

public BookBatteryListAdapter(Context context) {
    mContext = context;
}

public void setList(List<BookBattery> bookBatteryList) {
    mBookBatteryList.clear();
    mBookBatteryList.addAll(bookBatteryList);
    notifyDataSetChanged();
}

public void clear() {
    mBookBatteryList.clear();
    notifyDataSetChanged();
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View inflate = LayoutInflater.from(mContext)
            .inflate(R.layout.item_book_battery, parent, false);
    return new ViewHolder(inflate);
}

......

并且已经生成 setter 和 getter,我想问一下如何从服务器解析 json 到我的 class 然后我可以在 recyclerview 中显示到显示结果? ???

在这里您可以像这样解析您的 JSON 例如

JSONObject responseObj = new JSONObject(response);
JSONObject dataObj = responseObj.getJSONObject("data");

然后在这里,你可以通过解析数据对象中的字段来进行。

int id = dataObj.getInt("id");
String transactionStatus = dataObj.getString("transactionStatus");

同样,您可以对所有字段执行此操作。

然后给你设置数据Model Class Object

BookBattery bookBattery = new BookBattery();
bookBattery.setId(id);

同样,您可以为所有设置数据并将其添加到 List<BookBattery> 并将其设置为 RecyclerView Adapter