[英]Gson Parse Json with List<String>
我有一個 class 有一些字段:
public class GsonRepro {
class A {
private List<String> field1 = new ArrayList<>();
private String name;
private Integer status;
public A(){
}
public List<String> getfield1() { return field1; }
public void setField1(List<String> field1) { this.field1 = field1; }
public String getName() { return name; }
public void setName() { this.name = name; }
public Integer getStatus() { return status; }
public void setStatus(int status) { this.status = status; }
}
public static void main(String[] args) {
String str = "{\"name\":\"my-name-1\",\"status\":0,\"field1\":[\"0eac6b1d3d494c2d8568cd82d9d13d5f\"]}";
A a = new Gson().fromJson(str, A.class);
}
}
所有字段都被解析,但List<String> field1
,我怎樣才能讓它工作?
解決方案:
上面的代碼工作得很好。 最初,我只是在列表字段中輸入了一個錯字。
您可以嘗試在另一個問題的答案中使用 TypeToken 。
對你來說,它看起來像這樣:
import java.lang.reflect.Type;
import com.google.gson.reflect.TypeToken;
...
Type type = new TypeToken<A>(){}.getType();
A a = new Gson().fromJson(str, type);
問候
我嘗試使用您共享的上述代碼,並且工作正常,沒有任何問題。 請檢查以下代碼並驗證,
public static void main(String[] args) {
String str = "{\"name\":\"my-name-1\",\"status\":0,\"field1\":[\"0eac6b1d3d494c2d8568cd82d9d13d5f\"]}";
A a = new Gson().fromJson(str, A.class);
System.out.println(a.getName());
System.out.println(a.getStatus());
System.out.println(a.getfield1());
}
以下是正在控制台上打印的 output,
my-name-1
0
[0eac6b1d3d494c2d8568cd82d9d13d5f]
import java.util.ArrayList;
import java.util.List;
import com.google.gson.Gson;
class A {
private List<String> field1 = new ArrayList<>();
private String name;
private Integer status;
public A() {
}
public List<String> getfield1() {
return field1;
}
public void setField1(List<String> field1) {
this.field1 = field1;
}
public String getName() {
return name;
}
public void setName() {
this.name = name;
}
public Integer getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
}
public class GsonParseList {
public static void main(String[] args) {
String str = "{'name':'my-name-1','status':0,'field1':['0eac6b1d3d494c2','d8568cd82d9d13d5f']}";
A a = new Gson().fromJson(str, A.class);
System.out.println(a.getfield1());
}
}
Idk 對你有幫助,但我在下面有這樣的要求
public void getCheckPointLocationViaQRCode(String QRResource) {
RequestModel<String> req = new RequestModel<String>();
req.data = QRResource;
AppRepository.get_instance().apiHandler.post(AppRepository.get_instance().BASE_URL + "workorder/validateQR", req, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
// Something went wrong
}
@Override
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
if (response.isSuccessful()) {
try {
requireActivity().runOnUiThread(() -> {
//MODEL OLUŞTUR SONRA BAŞLA
String result = null;
try {
result = Objects.requireNonNull(response.body()).string();
} catch (IOException e) {
e.printStackTrace();
}
Gson gson = new Gson();
Type collectionType = new TypeToken<ResponseModel<WorkOrderLocation>>() {
}.getType();
ResponseModel<WorkOrderLocation> resultResponseModel = gson.fromJson(result, collectionType);
System.out.println("DÖNEN SONUCUM" + resultResponseModel.data.title);
ImageView success = new ImageView(requireActivity());
AlertDialog.Builder builder =
new AlertDialog.Builder(requireActivity()).
setMessage("QR KOD OKUMA BAŞARILI")
.setPositiveButton("DEVAM ET", (dialogInterface, i) -> {
dialogInterface.dismiss();
Bundle arguments = new Bundle();
arguments.putString("QRResult", resultResponseModel.data.latitude + " " + resultResponseModel.data.longitude);
MyPatrolFragment myPatrolFragment = new MyPatrolFragment();
myPatrolFragment.setArguments(arguments);
FragmentTransaction fragmentTransaction = getParentFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragmentContainerView, myPatrolFragment).addToBackStack("tag").commit();
}).
setView(success);
builder.show();
});
} catch (Exception e) {
e.printStackTrace();
System.out.println(e);
}
} else {
Toast.makeText(requireActivity(), "İşlem Başarısız Lütfen Tekrar Deneyin", Toast.LENGTH_SHORT).show();
}
}
});
}
我是這樣用的 希望對你有幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.