Deserialize Json and place values in views

Asked

Viewed 163 times

3

Well, what I want to do is an Activity where a Json is consumed. The problem is that all tutorials show only how to return a Json in a listview. What I want is a structure more or less like this:

  1. Title : Textview;
  2. Imagery : Imageview;
  3. Text : Textview;

This coming from a single Json, as in the example:

{ 
 "item":[
      { "titulo":"Recomendação",
        "imagem":"http://i.imgur.com/Bl0jBTc.png",
        "texto":"Texto",
      }
     ]
 }

I currently use Picasso to upload images. What a good way to do it?

  • @Thiagoluizdomacoski use it nowadays. What I need is to know how I would display this json on a simple screen, because all the tutorials I’ve seen just show you how to display json in a listview

  • 1

    Just to add knowledge, see also the Glide which is also very good.

  • @Acklay told me about Glide and Picasso, so I chose Picasso. But after I had already done everything I went to see and it seems that Glide is better, accepts gifs, etc. I think I will replace Picasso when I have time.

  • @Marceloawq think I’ve tended what you want to put on without time now. Have you been using with.google.gson.Gson? Later if no one has answered, I put an answer here.

  • @Acklay no, in the tutorial that I followed it shows with org.json

  • The Gson helps a lot saw! Then take a look too.

  • @Acklay will look yes. I’m starting to study about Json and everything is welcome, thanks for the tip!

  • @Thiagoluizdomacoski corrected

Show 3 more comments

4 answers

4


First you need to deserialize Json to get each of the parts.

String json = "{ \"item\":[{ \"titulo\":\"Recomendação\",\"imagem\":\"http://i.imgur.com/Bl0jBTc.png\", \"texto\":\"Texto\"} ]}";
String titulo;
String imagem;
String texto;
try {
    JSONObject jsonObject = new JSONObject(json);
    JSONArray jsonArray = jsonObject.getJSONArray("item");

    JSONObject jsonArrayJSONObject = jsonArray.getJSONObject(0);
    titulo = jsonArrayJSONObject.getString("titulo");
    imagem = jsonArrayJSONObject.getString("imagem");
    texto = jsonArrayJSONObject.getString("texto");
} catch (JSONException e) {
    e.printStackTrace();
}

Use the string image to download the image.
The others put them in the respective Textview.

  • perfect. Exactly what I needed

2

Try it this way:

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import org.json.JSONArray;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity {



     private final String JSON_CONTENT = "{ \"item\": [{ \"titulo\": \"Recomendação\", \"imagem\": \"http://i.imgur.com/Bl0jBTc.png\", \"texto\": \"Texto\" }] }";

    TextView titulo;
    TextView texto;
    ImageView imagem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Carregando os elementos da tela
        titulo = TextView.class.cast(findViewById(R.id.titulo));
        texto = TextView.class.cast(findViewById(R.id.texto));
        imagem = ImageView.class.cast(findViewById(R.id.imagem));


        try{
            // transformamos a String em JsonObjetc (provavelmente, aqui voce deve pegar da web)
            final JSONObject json = new JSONObject(JSON_CONTENT);
            // Pegamos a lista item!
            final JSONArray lista = json.getJSONArray("item");
            // neste caso, vamos pegar o primeiro item da lista!
            final JSONObject jsonObject = lista.getJSONObject(0);
            // Setamos o titulo
            titulo.setText(jsonObject.getString("titulo"));
            // Setamos o texto
            texto.setText(jsonObject.getString("texto"));
            //adicionamos a imagem através do picasso
            Picasso.with(getApplicationContext()).load( jsonObject.getString("imagem")).into(imagem);
        }catch (final Exception e){
            e.printStackTrace();
        }
    }

}
  • 1

    @ramaral gave a similar suggestion. I did it here and it worked, basically the way you did it.

0

Receives a JSON similar to this:

 { 
     "itens":[
          { "titulo":"Titulo 0001",
            "texto":"Lorem ipsum dolor sit amet, consectetur ",
            "imagem":"http://seurepositorio/p/imagem1.png" },

          { "titulo":"Titulo 0002",
             "texto":"adipiscing elit, sed do eiusmod tempor ",
             "imagem":"http://seurepositorio/p/imagem2.png" }

           ....
           e segue...

         ]
   }

And make another request to fetch the images a code like this:

URL url = new URL(imagem);
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
imageView.setImageBitmap(bmp);
  • I think the difficulty of AP is to deserialize the json and put in the layout.

  • That way I’d have to put it on a listview, which is what I don’t want. What I need is that you only have one "items" and instead of putting it in a listview, put it in the layout as said in the question

  • Hmm. You make the JSON request, with a specific filter, such that you return only one item, place the title and text result in TEXTVIEWS and request the image to load it in an IMAGEVIEW.

-1

json would come title, image url, and text. If it’s a list, you need to create a listview with an adpter to work as you need it, now if it’s a simple screen, you just need to deserialize the json and put it in your layout.

{"titulo":"seu titulo", "imagem":"http://urlimagem.com/imagem.jpg", "texto":"seu texto"}
  • 1

    I think the difficulty of AP is to deserialize the json and put in the layout.

  • To desiccate can do in the nail, taking the json, transforming into object and put in the layout). Or use libraries like retrofit, voley, etc. .

  • What I meant is if the answer doesn’t tell you how to deserialize json and put it in the layout it won’t help AP (no answer to the question)

Browser other questions tagged

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