Kote22
Kote22

Reputation: 27

Deserialize Json with Gson to display information in TextViews

I've been trying for several days to deserialize a JSON like this with GSON.

 {
"upcoming": [
    "silver",
    "gold",
    "silver",
    "silver",
    "silver",
    "silver",
    "gold",
    "silver",
    "silver"
],
"superMagical": 559,
"magical": 19,
"legendary": 79,
"epic": 699,
"giant": 94
 }

Do you know how I can show this information in different TextViews?

I've managed to Deserialize other simpler Json's and show their information in TextViews, but this one is giving me headaches.

My idea was to make a class called Chests and this would be the code:

public class chests implements Serializable {


public int superMagical;
public int magical;
public int legendary;
public int epic;
public int giant;
}

Then, in another class, I would do the following

public class ClashChest implements Serializable {

@SerializedName("")
@Expose
public chests chestData; //

List<Upcoming> upcoming;
}

And finally in the activity where I want the information to be shown I would do this

public class ChestActivity extends AppCompatActivity {

public ClashChest datachest;
public TextView chest1, chest2, chest3, chest4;

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

    datachest = (ClashChest)getIntent().getSerializableExtra("cofres");

    chest1 = findViewById(R.id.chest1txt);
    chest2 = findViewById(R.id.chest2txt);
    chest3 = findViewById(R.id.chest3txt);
    chest4 = findViewById(R.id.chest4txt);

    chest1.setText("" + datachest.chestData.legendary);

}
}

To pass information between activities I have the button with the following code

 btnCofre.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (!inputUser.getText().toString().isEmpty()) {


                OkHttpClient client = new OkHttpClient();


                StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                StrictMode.setThreadPolicy(policy);

                Request request = new Request.Builder()
                        .url("https://api.royaleapi.com/player/" + inputUser.getText().toString() + "/chests")
                        .get()
                        .addHeader("auth", "Whatever")
                        .build();


                try {

                    Response response = client.newCall(request).execute();
                    String json = response.body().string();



                    if (response.isSuccessful()) {


                        Gson gson = new Gson();

                        ClashChest clashChest = gson.fromJson(json, ClashChest.class);

                        Intent intent;
                        intent = new Intent(MainActivity.this, ChestActivity.class);
                        intent.putExtra("cofres", clashChest);
                        startActivity(intent);


                    } 




    });

Upvotes: 0

Views: 95

Answers (1)

Macmist
Macmist

Reputation: 723

I would say create a class that match your object, then let GSON do the deserialization. You have this:

 {
"upcoming": [
    "silver",
    "gold",
    "silver",
    "silver",
    "silver",
    "silver",
    "gold",
    "silver",
    "silver"
],
"superMagical": 559,
"magical": 19,
"legendary": 79,
"epic": 699,
"giant": 94
 }

An equivalent class would be:

public class Foo {
    List<String> upcoming;
    int superMagical;
    int magical;
    int legendary;
    int epic;
    int giant;

    // eventually other functions, getter/setter...
}

then to deserialize it:

Foo targetObject = new Gson().fromJson(yourJson, Foo.class);

Finally you can display it in views with:

yourTextview.setText(String.valueOf(targetObject.yourField)) // for the integers
yourTextview.setText(String.join(",", targetObject.upcoming)); // displays all values from upcoming separated by a coma

for the string you might also want to use a list view

Edit: For what I see from the code you have added, your ClashChest class doesnt match the json you are trying to parse. When converting to json, you can consider that a class will be represented as a json object. So here your class ClashChest would look like something like this once converted to JSON:

public class ClashChest implements Serializable {

@SerializedName("")
@Expose
public chests chestData; //

List<Upcoming> upcoming;
}

will be

{
    "chestData": 
    {
        // all the fieds you have in your chests class
    },
    "upcoming": // an array that contains x upcoming objects
    [
        {
            // every field you have in your Upcoming class
        }
    ]
}

If you want to match your json, you should put everything in one place. And the upcoming array contains only strings, not objects, so if you want to match your object it would be something like this:

public class Chest implements Serializable {
    List<String> upcoming;
    int superMagical;
    int magical;
    int legendary;
    int epic;
    int giant;
}

Upvotes: 1

Related Questions