joe joe - 4 months ago 10
JSON Question

need to save all the values coming in for loop?

if (Username.equals(RegisteredUSer)) {
JSONArray nameObject = msg.getJSONArray("NameList");

for (int j = 0; j < nameObject.length(); j++) {
JSONObject name = nameObject.getJSONObject(j);
USername = name.getString("FirstName");
USerweight = name.getString("Weight");
Weight = Double.parseDouble(USerweight);
}
}


HERE i need to save all the username and weight coming within for loop and convert the userweight in double ,and extracting the save values within the same class??

PSo PSo
Answer

Your version:

ArrayList<String> username = new ArrayList<String>(); 
ArrayList<Double> weight = new ArrayList<Double>(); 

if (Username.equals(RegisteredUSer)) {
    JSONArray nameObject = msg.getJSONArray("NameList");

    for (int j = 0; j < nameObject.length(); j++) {
        JSONObject name = nameObject.getJSONObject(j);
        USername        = name.getString("FirstName");
        USerweight      = name.getString("Weight");
        Weight          = Double.parseDouble(USerweight);
        username.add(USername);
        weight.add(Weight);
    }
}