-1

i need to convert this JSON to a Java Object:

{"estabelecimento":[{"id":"5","idUsuario":"5","razaoSocial":"Bibi LTDA","nomeFantasia":"BibiPizza","telefone":"22121212","email":"[email protected]","gostaram":"0"},{"id":"8","idUsuario":"1","razaoSocial":"Nestor Latuf LTDA","nomeFantasia":"Nestor Sorvetes","telefone":"32343233","email":"[email protected]","foto":"","gostaram":"0"},{"id":"9","idUsuario":"1","razaoSocial":"Comercio Alimenticio Rivaldo","nomeFantasia":"Rogers Burguer","telefone":"210021020","email":"[email protected]","foto":"","gostaram":"0"}]}

I try this, but not work:

 //JSONArray jArr = new JSONArray(br.toString());  
        
        //JSONObject jObj = new JSONObject(br.toString());
        
        //JSONArray jArr = jObj.getJSONArray("list");
        
        JSONArray jArr = new JSONArray(br.toString());

        for (int i=0; i < jArr.length(); i++) {
            JSONObject obj = jArr.getJSONObject(i);
            estabelecimento.setId(obj.getLong("id"));
            estabelecimento.setIdUsuario(obj.getLong("idUsuario"));
            estabelecimento.setRazaoSocial(obj.getString("razaoSocial"));
            estabelecimento.setNomeFantasia(obj.getString("nomeFantasia"));
            estabelecimento.setTelefone(obj.getString("telefone"));
            estabelecimento.setEmail(obj.getString("email"));
            estabelecimento.setGostaram(obj.getInt("gostaram"));
            
            estabelecimentoList.add(estabelecimento);
        }
        con.disconnect();

How can i obtain a Java Object? Someone can help? tks.

2
  • You don't say which JSON libraries you use and under which operating system you work. Plus that there are tons of examples for this published elsewhere. Commented May 25, 2014 at 1:25
  • If it's a String it already is a Java object. Did you want to be any more specific than that? Commented May 25, 2014 at 2:28

2 Answers 2

2

You can use the Gson lib of google:

public class MyClass {

    private int data1 = 100;
    private String data2 = "hello";
    private List<String> list = new ArrayList<String>() {
      {
        add("String 1");
        add("String 2");
        add("String 3");
      }
    };

    //getter and setter methods needed


}


String str = {"data1":100,"data2":"hello","list":["String 1","String 2","String 3"]};
com.google.gson.Gson gson = new com.google.gson.Gson();

//To convert json string to class use fromJson
MyClass obj = gson.fromJson(str, MyClass .class);

//To convert class object to json string use toJson
String json = gson.toJson(obj);
Sign up to request clarification or add additional context in comments.

2 Comments

Yes Gson will do what you are searching for. Look up Gson on google.
Thanks man! But now i have other problem: 05-26 12:31:29.128: W/System.err(1514): org.json.JSONException: Value java.io.BufferedReader@40548f20 of type java.lang.String cannot be converted to JSONObject I will post in another question. tks!
0

At high level two major steps:

  1. Generate a Java class from your JSON, e.g. by using this generator or similar: http://www.jsonschema2pojo.org/
  2. Use Jackson processor to deserialize your JSON file:

     ObjectMapper mapper = new ObjectMapper();
     YourGeneratedClass obj = (YourGeneratedClass) mapper.readValue(new File("path-to-your-json-file"), YourGeneratedClass.class);
    

More about Jackson: http://jackson.codehaus.org/

You can also create YourGeneratedClass manually if you feel comfortable enough with this.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.