Deserialize string json in obejto on android

Asked

Viewed 43 times

0

I would like to transform this string into an object, alias, put the data of the jsom string into an object. I’m not succeeding, I’ve already received the code 200 of success and I also see that I receive the values according to the string below, ms can’t mount on an object

{
"@odata.context": "https://was-p.bcnet.bcb.gov.br/olinda/servico/PTAX/versao/v1/odata$metadata#_CotacaoDolarDia",
"value": [
    {
        "cotacaoCompra": 3.9572,
        "cotacaoVenda": 3.9578,
        "dataHoraCotacao": "2019-05-10 13:08:45.718"
    }
]

}

1 answer

0

You get the JSON as string and send it to an object? you can do so:

 try{

    JSONObject jsonResult = new JSONObject( result.toString() );
    JSONArray Jarray = jsonResult.getJSONArray( "data" );

    if ( Jarray.length() != 0 ) 
    {
        Objeto objeto = new Objeto();
        for ( int i = 0; i < Jarray.length(); i++ ) 
        {
            JSONObject Jasonobject = Jarray.getJSONObject( i );
            objeto.setaCampo( Jasonobject.getString( "campo" ) );
            ObjetoController.setObjeto( objeto );
        }
    }

} 
catch ( Exception e )
{
    Log.e( "error", e.getMessage() );
}

But it could be a list too:

List<Object[]> lista = new ArrayList<>();

The idea is the same, but in the loop for would put something like:

Object[] ob = new Object[ 1 ];
ob[ 0 ] = Jasonobject.getString( "campo" );
lista.add( 0, ob );

Browser other questions tagged

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