简体   繁体   中英

How to parse JSON array objects textview 1 and textview 2

My code works for parsing json objects to a single textview now I want to change it to parse the first object to text Response and the second to text Response2 thats why I changed the for loop to i < = 1 so I can get only the first 2 results from the array I dont need the rest. the for loop works on 1 field but I think there is something wrong with my if /else loop

try {
        // Parsing json array response
        // loop through each json object
        jsonResponse = "";
        jsonResponse2 ="";
        for (int i = 0; i <= 1; i++) {
          //int i  <= 1
            JSONObject person = (JSONObject) response
                            .get(i);

            String name = person.getString("name");

                if (i <=0){

                    jsonResponse += "Name: " + name + "\n\n";
                }

                else { jsonResponse2 += "Name: " + name + "\n\n";}

                }

            txtResponse.setText(jsonResponse);
            txtResponse2.setText(jsonResponse2);

JSON

[
    {
        "name": "Ravi Tamada",
        "email": "ravi8x@gmail.com",
        "phone": {
            "home": "08947 000000",
            "mobile": "9999999999"
        }
    },
    {
        "name": "Tommy",
        "email": "tommy@gmail.com",
        "phone": {
            "home": "08946 000000",
            "mobile": "0000000000"
        }
    },
    {
        "name": "Roy",
        "email": "roy8@gmail.com",
        "phone": {
            "home": "01944000000",
            "mobile": "6600000000"
        }
    },
    {
        "name": "Sami",
        "email": "sami69@gmail.com",
        "phone": {
            "home": "08006 104400",
            "mobile": "7700000000"
        }
    }
]

May this resolve your problem:

                String name1,name2,name3,name4;
                JSONArray jArray=new JSONArray(result);
                for(int i=0;i<jArray.length();i++){
                    if(i==0){
                        name1=jArray.getJSONObject(i).getString("name");
                        Log.e("Name First", name1);
                    }else if(i==1){
                        name2=jArray.getJSONObject(i).getString("name");
                        Log.e("Name Second", name2);
                    }else if(i==2){
                        name3=jArray.getJSONObject(i).getString("name");
                        Log.e("Name Third", name3);
                    }else if(i==3){
                        name4=jArray.getJSONObject(i).getString("name");
                        Log.e("Name Four", name4);
                    }
                }
               // txtResponse.setText(name1);
               // txtResponse2.setText(name2);

Where "result" is your json array string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM