Read Json without "knowing" the JAVA keys

Asked

Viewed 638 times

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());
  • Your question is a bit confusing. You want to let the user fill in what occupation for you?

  • the user will not fill any occupation simply want to read the json without specifying the keys! the user will not register anything

  • The JSON is in the format you specified and just below the user will add the properties? (writer, cool... etc?)

  • Sorry I’m a little new in this world,

  • the problem is this

  • 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

  • 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).

Show 3 more comments

1 answer

1

The class JSONObject has a method called keySet listing the keys to an object:

JSONObject profissoes = (JSONObject) jsonObject.get("profissao");

chaves = profissoes.keySet();

for (String chave : chaves) {
  System.out.println(chave);
}
  • thanks my partner! tried but no results! Thank you very much he seems to have error in line

  • keys = json.getJSONObject("professional"). keySet();

  • @rodrigogabriel can place the Mports?

  • @rodrigogabriel changed my example to use the same library you are using. Make sure it works for you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.