2

I'm trying to parse a JSON object. The JSON response is as follows.

{
  "message": "Success",
  "list": [
    {
      "orderId": 24,
      "phoneNumber": "1234567893",
      "totalAmount": 100,
      "addressBean": {
        "cadId": 1,
        "phone2": "1234567899",
        "address1": "34, gandhi nagar",
        }
}, 

Android code which i have tried for getting "orderId", "phoneNumber" and "totalAmount" is below.

final List<OrderModel> orderList = new ArrayList<>();

    public void onResponse(JSONObject response) {
                    try {

                        if (response.getString("message").equalsIgnoreCase("Success")) {
                            JSONArray jArray = response.getJSONArray("list");

                            for (int i = 0; i < jArray.length(); i++) {

                                OrderModel model = new OrderModel();
                                orderModel.setOrderId(jArray.getJSONObject(i).getString("orderId"));
                                orderModel.setphoneNumber(jArray.getJSONObject(i).getString("phoneNumber"));
                                orderModel.setTotalAmount(new BigDecimal(jArray.getJSONObject(i).getString("totalAmount")));
                                 orderList.add(orderModel);

                            }
                            setOrderList(orderList);

I want to show the "cadId", "phone2" and "address1" in a Textview. How can i do that?

2 Answers 2

3

Try to use this Code

try {
        JSONArray jArray = response.getJSONArray("list");
        for (int i = 0; i < jArray.length(); i++) {
            JSONObject jsonObject = jArray.getJSONObject(i);
            if (jsonObject.has("addressBean")){
                JSONObject addressObject = jsonObject.getJSONObject("addressBean");
                int cadId = addressObject.getInt("cadId");
                String phone = addressObject.getString("phone2");
                String address = addressObject.getString("address1");
            }
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
Sign up to request clarification or add additional context in comments.

Comments

1

Code for phone2

String phone2 = jArray.getJSONObject(i).getJSONObject("addressBean").getString("phone2");

Use getInt() for cadId

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.