gpt4 book ai didi

java - 用Gson反序列化Json,在TextViews中显示信息

转载 作者:行者123 更新时间:2023-12-02 10:47:03 25 4
gpt4 key购买 nike

我几天来一直在尝试使用 GSON 反序列化这样的 JSON。

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

你知道如何在不同的 TextView 中显示此信息吗?

我已经成功地反序列化其他更简单的 Json 并在 TextView 中显示它们的信息,但这一个让我头疼。

我的想法是创建一个名为 Chests 的类,代码如下:

public class chests implements Serializable {


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

然后,在另一个类中,我会执行以下操作

public class ClashChest implements Serializable {

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

List<Upcoming> upcoming;
}

最后在我想要显示信息的 Activity 中我会这样做

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);

}
}

为了在 Activity 之间传递信息,我有带有以下代码的按钮

 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);


}




});

最佳答案

我会说创建一个与您的对象匹配的类,然后让 GSON 进行反序列化。你有这个:

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

等效的类是:

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

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

然后反序列化它:

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

最后你可以在 View 中显示它:

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

对于字符串,您可能还想使用 ListView

编辑:根据我从您添加的代码中看到的情况,您的 ClashChest 类与您尝试解析的 json 不匹配。当转换为json时,你可以认为一个类将被表示为一个json对象。因此,一旦转换为 JSON,您的 ClashChest 类将如下所示:

public class ClashChest implements Serializable {

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

List<Upcoming> upcoming;
}

将会

{
"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
}
]
}

如果你想匹配你的json,你应该把所有东西放在一个地方。即将到来的数组仅包含字符串,而不包含对象,因此如果您想匹配您的对象,它将是这样的:

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

关于java - 用Gson反序列化Json,在TextViews中显示信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52468558/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com