Convert a JSON String to a JAVA Object Array

Asked

Viewed 1,800 times

1

Hello, I’m not getting the conversion, my string is coming like this.

{
  "empresa1":
  {
    "category_id" : "Item 1",
    "id" : 1,
    "imagePath" : "imagem",
    "name" : "empresa 1",
    "short_desc" : "desc da empresa 1"
  },
  "empresa2":
  {
    "category_id" : "Item 1",
    "id" : 2,
    "imagePath" : "imagem",
    "name" : "empresa2",
    "short_desc" : "desc da empresa 2"
  },
  "empresa3":
  {
    "category_id" : "Item 1",
    "id" : 3,
    "imagePath" : "imagem",
    "name" : "empresa3",
    "short_desc" : "desc da empresa 3"
  }
}

I need to convert this text to an array of my object and then populate my table.

Thank you.Registros

  • Want to convert to a company class or just a map containing key and value?

  • I need to convert this data to an array of my company class, what is making me difficult is that the string comes with the "root" of each registration (company1, company2, company3), remembering that this root is me who type the name.

1 answer

1

First you will need to create a class called Empresa, follow:

public class Empresa {

    private Integer id;
    private String idCategoria;
    private String imagePath;
    private String name;
    private String shortDesc;

    public Integer getId() {return id;}
    public void setId(Integer id) {this.id = id;}

    public String getIdCategoria() {return idCategoria;}
    public void setIdCategoria(String idCategoria) {this.idCategoria = idCategoria;}

    public String getImagePath() {return imagePath;}
    public void setImagePath(String imagePath) {this.imagePath = imagePath;}

    public String getName() {return name;}
    public void setName(String name) {this.name = name;}

    public String getShortDesc() {return shortDesc;}
    public void setShortDesc(String shortDesc) {this.shortDesc = shortDesc;}
}

Now create a List of the kind Empresa and a JSONObject passing the json who is in String as a parameter.

String jsonString = "{ \"empresa1\": { \"category_id\": \"Item 1\", \"id\": 1, \"imagePath\": \"imagem\", \"name\": \"empresa 1\", \"short_desc\": \"desc da empresa 1\" }, \"empresa2\": { \"category_id\": \"Item 1\", \"id\": 2, \"imagePath\": \"imagem\", \"name\": \"empresa2\", \"short_desc\": \"desc da empresa 2\" }, \"empresa3\": { \"category_id\": \"Item 1\", \"id\": 3, \"imagePath\": \"imagem\", \"name\": \"empresa3\", \"short_desc\": \"desc da empresa 3\" } }";

JSONObject jsonObjectEmpresas = new JSONObject(jsonString);

List<Empresa> listaDeEmpresas = new ArrayList<Empresa>();

After doing this, there are a few ways you can go through the keys and get their respective values. I will post two examples.

Using the interface Iterator:

Iterator<String> iteratorEmpresas = jsonObjectEmpresas.keys();
while (iteratorEmpresas.hasNext()) {
    JSONObject dadosEmpresa = jsonObjectEmpresas.getJSONObject(iteratorEmpresas.next());

   Empresa empresa = new Empresa();
        empresa.setId(dadosEmpresa.getInt("id"));
        empresa.setName(dadosEmpresa.getString("name"));
        empresa.setIdCategoria(dadosEmpresa.getString("category_id"));
        empresa.setImagePath(dadosEmpresa.getString("imagePath"));
        empresa.setShortDesc(dadosEmpresa.getString("short_desc"));

        listaDeEmpresas.add(empresa);
}

Using the interface Set:

for (String keyEmpresa : jsonObjectEmpresas.keySet()) {
    JSONObject dadosEmpresa = jsonObjectEmpresas.getJSONObject(keyEmpresa);

        Empresa empresa = new Empresa();
        empresa.setId(dadosEmpresa.getInt("id"));
        empresa.setName(dadosEmpresa.getString("name"));
        empresa.setIdCategoria(dadosEmpresa.getString("category_id"));
        empresa.setImagePath(dadosEmpresa.getString("imagePath"));
        empresa.setShortDesc(dadosEmpresa.getString("short_desc"));

        listaDeEmpresas.add(empresa);
}

Going through the list of companies:

for (Empresa empresa : listaDeEmpresas) {
    System.out.println("-----");
    System.out.println("ID:" + empresa.getId());
    System.out.println("NAME:" + empresa.getName());
    System.out.println("ID CATEGORIA:" + empresa.getIdCategoria());
    System.out.println("IMG PATH:" + empresa.getImagePath());
    System.out.println("SHORT DESC:" + empresa.getShortDesc());
}

To work with json and parse automatically simply, I recommend using the library GSON or Jackson.

  • When I try to run Jsonobject jsonObjectEmpresas = new Jsonobject(jsonComercio); he error that they are incompatible types, saying that String cannot be converted to Boolean.

  • Your jsonComercio is a string?

  • Yes, it is a string - Response = firebase.get("Commerce"); String jsonComercio = Response.getRawBody();

  • Strange. What comes in System.out.println(jsonComercio)?

  • Exactly the answer I sent there in the first question { "empresa1": { "category_id" "Item 1", "id" 1, "imagePath" : "image", "name" : "company 1", "short_desc" : "desc da empresa 1" }, "empresa2": { "category_id" "Item 1", "id" 2, "imagePath" "image", "name" "empresa2", "short_desc" "enterprise 2" }, "empresa3": { "category_id" "Item 1", "id" 3, "imagePath" "image", "name" "empresa3", "short_desc" "enterprise desc 3" } }

  • The package you imported is org.json.Jsonobject?

  • import net.sf.json.Jsonobject; was a library I found with the name json-lib-2.4-jdk15

  • The correct jar can be downloaded from the Maven: http://central.maven.org/maven2/org/json/json/20160810/json-20160810.jar archive

  • It worked! But I need to take beyond the attributes, this root (empresa1, empresa2, empresa3), because I will use it to update the record, you know how? I even thought about creating a second class, with the same attributes of the companies class, but with an extra to save this field, but I don’t know how.

  • Yes, in the first example the root is the iteratorEmpresas.next() and in the second is the keyEmpresa variable.

  • I used the second example (keyEmpresa), I didn’t understand the logic of how it works but I managed to do it, I took the root and saved it in a second class to work with it, exactly what I needed, thank you very much Diego!!! : D

  • Good that it worked. If you can mark the answer as useful I thank you. Abs

  • I’ve scored as useful, but said that votes of users with less than 15 reputation are registered but do not change the score shown in the post. Thanks.

Show 8 more comments

Browser other questions tagged

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