2
The problem is that I need to inform the professions and wanted it to be automatic to read the json without having to inform ocup[1] = "jornalista"
. I would like the json reading to be automatic.
{
"profissao": {
"jornalista": [
"escritor",
"legal",
"fotografo"
],
"programador": [
"focado",
"exatas",
"articulado"
],
"maquinista": [
"senai",
"mecanico",
"articulado"
]
}
}
My code is this
JSONObject jsonObject = (JSONObject) obj;
//System.out.println(jsonObject);
JSONObject locs = (JSONObject) jsonObject.get("profissao");
String[] ocup = new String[4];
ocup[0] = "jornalista";
ocup[1] = "programador";
ocup[2] = "maquinista";
ArrayList respostas = new ArrayList();
Scanner scan = new Scanner(System.in);
String v;
int cont = 0;
while(cont < ocup.length){
JSONArray jorn = (JSONArray) locs.get(ocup[cont]);
//System.out.println(jorn);
int y= 0;
while(y < jorn.size()){
String name = jorn.get(y).toString();
System.out.println("digite s ou n você é "+name);
v = scan.nextLine();
if(v.equals("s")) {
respostas.add(name);
}
y++;
}
cont++;
}
System.out.println(respostas.size());
I think it might help you: Traverse an array without knowing its indices
– Marconi
Your question is a bit confusing. You want to let the user fill in what occupation for you?
– Sorack
the user will not fill any occupation simply want to read the json without specifying the keys! the user will not register anything
– rodrigo gabriel
The
JSON
is in the format you specified and just below the user will add the properties? (writer, cool... etc?)– Sorack
Sorry I’m a little new in this world,
– rodrigo gabriel
the problem is this
– rodrigo gabriel
I need to read the file, go through this json automatically to read it only, I don’t want to register anything in it, I just want to read it understand? the problem is that if I’m going to put one more profession in my json file, I have to go and put it in the ocup[] array, but I think there should be a more shaped way with less work to do this
– rodrigo gabriel
Has any response helped solve the problem and can address similar questions from other users? If so, make sure to mark the answer as accepted. To do this just click on the left side of it (below the indicator of up and down votes).
– Sorack