How to get data received in JSON and store in a class

0

Good afternoon!

I'm having trouble implementing the code to retrieve the data that is coming in JSON and store that data in a class.

If someone can help me thank you, this one already.

My idea is to get the data from the webservice and store this Json data in the user class because I will use this data to write to the application database.

Here is the snippet of my code

//Cria e inicializa um objeto Gson
Gson gsonUsuario = new Gson();

//Passa os dados que estão nas variaveis para o parametro
Transmissao_Env transmissao_env = new Transmissao_Env();
transmissao_env.setEmail(email);
transmissao_env.setSenha(senha);
transmissao_env.setFuncao(funcao);

//Realiza conexão com o servidor webservice e passa os parametros junto
String resultado = ToolBox.comunicacao("http://www.meusite.com.br/ws/index.php", gsonUsuario.toJson(transmissao_env));

//Pega o valor antes da tag WSTAG que foi recebida do webservice
String parRes[] = resultado.split("#WSTAG#");

//Verifica a quantidade de caracteres
switch (parRes.length) {
    case 2:

        //Verifica se o resultado do webservice foi gerado corretamente(0)
        if (parRes[0].equals("0")) {
            //Realiza a conversão do Json para um objeto do tipo
            Usuario mUsuario = gsonUsuario.fromJson(parRes[1], Usuario.class);

            //Neste ponto que preciso de ajuda
            mUsuarioDao = new UsuarioDao(mContext);
            mUsuarioDao.insereUsuario(mUsuario);

The data is being received in the result String and the Transition_Rec class has the ArrayList methods.

    
asked by anonymous 23.12.2015 / 14:59

1 answer

1

To convert data from JSON to object, I advise using the Gson you're using or libraries like Retrofit that allow you to request and directly map Json to an object. But I suggest that in a first phase you parse "manually" using the class JSONObject and you can more control over all elements. A simple example of how to do this:

Imagine that we have a String with the response of a user

"{" user ": {" name ":" john "," age ": 19}

and that we have a class User with the same fields. To do the parsing: 1- Create a Json Object of the response by passing the string:  JSONObject response = new JSONObject (response);

2- We get the user object: JSONObject user = response.getJSONObject ("user"); Note that in this second call we use the first object of the response and use the object key to get the object

3- We can get the name and age by doing String name = user.getString ("name"); thereafter

4- With the attributes of our extracted object, just create an instance of the User and that's it.

I went through a lot of this process because it helps to realize errors made many times when using libraries where for example put one of the attributes in the class with the different name of the keys in JSON that can result in an error.

See this code / the> I wrote a few months ago to make the parse of JSON's a bit more complicated

    
25.12.2015 / 01:28