{
"clientes":[
{
"codigo":"1341",
"empresa":0,
"nombre":"asd",
"e_mail":"cli@cli.com"
},
{
"codigo":"5435",
"empresa":0,
"nombre":"asd",
"e_mail":"cli@cli.com"
}
]
}
I am trying to extract the keys and values from a json then in an array I put all the keys and in another array all the values. The problem now is that more than 1 client can come, so I have to make a list of arrays of keys, and a list of arrays of values
As you can see, in the output the values of position [0] are stepped on with that of position [1] and it shouldn't be like that, because in the "codigo" field I send different values ..
ArrayList listArrayKeys = new ArrayList();
ArrayList listArrayValues = new ArrayList();
ArrayList<String> arrayKeys = new ArrayList<>();
ArrayList<String> arrayValues = new ArrayList<>();
DatabaseHelper databaseHelper = new DatabaseHelper(this);
JSONObject jsonObject;
String tableName;
void getKeysAndValues() throws JSONException {
try {
Iterator<String> keys = jsonObject.keys();
while(keys.hasNext()) {
String key = keys.next();
System.out.println(key);
if(jsonObject.get(key) instanceof JSONArray) {
JSONArray array = (JSONArray) jsonObject.get(key);
for(int i = 0; i < array.length(); i++){
JSONObject object = (JSONObject) array.get(i);
Iterator<String> innerKeys = object.keys();
arrayValues.clear();
while(innerKeys.hasNext()) {
String innerKey = innerKeys.next();
String innerValue = object.getString(innerKey);
arrayKeys.add(innerKey);
arrayValues.add(innerValue);
System.out.println(innerKey);
}
listArrayKeys.add(arrayKeys);
listArrayValues.add(arrayValues);
}
Log.d("ARRAYVALUES", String.valueOf(listArrayValues));
}
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
output:
[[5435, 0, asd, cli@cli.com], [5435, 0, asd, cli@cli.com]
should be:
[[1341, 0, asd, cli@cli.com], [5435, 0, asd, cli@cli.com]