0

How I can get only the "name" string of every object under "fields" Array, of main Array index at 0 & then next index using loop or with something super idea

[
    {
        "name": "Bank1",
        "fields": [
            {
                "name": "Email",
                "slug": "email",
                "type": "input"
            },
            {
                "name": "City",
                "slug": "city",
                "type": "input"
            },
            {
                "name": "Screenshot",
                "slug": "screenshot",
                "type": "file"
            },
            {
                "name": "Full Name",
                "slug": "full-name",
                "type": "input"
            }
        ],
        "status": "Active"
    },
    {
        "name": "Bank2",
        "fields": [
            {
                "name": "Email",
                "slug": "email",
                "type": "input"
            },
            {
                "name": "City",
                "slug": "city",
                "type": "input"
            },
            {
                "name": "Screenshot",
                "slug": "screenshot",
                "type": "file"
            },
            {
                "name": "Submitted Date",
                "slug": "submitted-date",
                "type": "calendar"
            }
        ],
        "status": "Active"
    }
]

Output I want:
Email
City
Screenshot
Full Name

Means in the output, I have got index 0, first object array data...

What I have done yet

public void onResponse(String response) {
                        try {
                            JSONArray jsonArray = new JSONArray(response);


                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject jsonObject = jsonArray.getJSONObject(i);

                               
                                String p_name = jsonObject.getString("name");

                                 ArrayList<String> arr = new ArrayList<>();

                                JSONArray ja = jsonObject.getJSONArray("fields");
                                int len = ja.length();

                                for (int j = 0; j < len; j++) {
                                    JSONObject json = ja.getJSONObject(j);
                                    arr.add(json.getString("name"));
                                }


}}catch block...

this gives me all indexes name data i want only specific index data
My Current Output:
Email
City
Screenshot
Full Name
Email
City
Screenshot
Submitted Date

2
  • Use Hash map for storing both index and its value name Commented Aug 25, 2021 at 5:25
  • how can you show me an example? @Piyush Commented Aug 25, 2021 at 5:35

2 Answers 2

1

If you want to get specific index data then you should pass if condition in for loop.

EX: to get output of index 0 like,

Email

City

Screenshot

Full Name

Your code would be like below.

public void onResponse(String response) {
                    try {
                        JSONArray jsonArray = new JSONArray(response);


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

                            JSONObject jsonObject = jsonArray.getJSONObject(i);

                           
                            String p_name = jsonObject.getString("name");

                             ArrayList<String> arr = new ArrayList<>();

                            JSONArray ja = jsonObject.getJSONArray("fields");
                            int len = ja.length();

                            for (int j = 0; j < len; j++) {
                                JSONObject json = ja.getJSONObject(j);
                                arr.add(json.getString("name"));
                            }

                           }
 }}catch block...
Sign up to request clarification or add additional context in comments.

6 Comments

but there will be infinite index 0,1,2,3.... so how can i do for that?
but I think you said that you want only specific index data.(So, I mean your problem is when you add into list there you need to determine particular index)
So, for that you may need to create model class.
No I want only specific data on specific index do you have any solution pls help me
you want to get all data but on other side you want only specific index data when you need right?
|
0
               int i=gotIndex;//here you can give your exact index that you want
                            JSONObject jsonObject = jsonArray.getJSONObject(i);

                           
                            String p_name = jsonObject.getString("name");

                             ArrayList<String> arr = new ArrayList<>();

                            JSONArray ja = jsonObject.getJSONArray("fields");
                            int len = ja.length();

                            for (int j = 0; j < len; j++) {
                                JSONObject json = ja.getJSONObject(j);
                                arr.add(json.getString("name"));
                            }

here you used the loop you can use the json object out side the loop with using the index number i (jsonObject = jsonArray.getJSONObject(i))

9 Comments

but i want next array data which means index 1 after 0 data thats why i used loop any solution? @AjayKS
what is your real problem?
see question I want specific index "fields" array data, under that only name string of objects
see now you will understand i have updated my question @AjayKS
I think the code is correct, and your printing method or something is incorrect, the logic you mentioned is correct. the implementation method is incorrect.
|

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.