gpt4 book ai didi

Java 如何将类> 传递给方法?

转载 作者:行者123 更新时间:2023-11-29 09:28:40 25 4
gpt4 key购买 nike

我得到 Json 字符串,我想创建将 Json 字符串转换为对象的通用方法。我创建对象

public class ResponseMessage<T> {

private T result;
private ResultCode resultCode;
private String details;

public ResponseMessage(T result, ResultCode resultCode, String details) {
this.result = result;
this.resultCode = resultCode;
this.details = details;
}

public T getResult() {
return result;
}

public void setResult(T result) {
this.result = result;
}

public ResultCode getResultCode() {
return resultCode;
}

public void setResultCode(ResultCode resultCode) {
this.resultCode = resultCode;
}

public String getDetails() {
return details;
}

public void setDetails(String details) {
this.details = details;
}
}

我想将泛型类型传递给我的类并返回对象。但我不知道该怎么做。我创建类 Converter

public class Converter {
private static Converter instance;
private Gson gson = new Gson();

public static Converter me() {
if (instance == null) {
instance = new Converter();
}
return instance;
}

public <T> T getModel(String jsonStr, Class<T> type) {
T t = gson.fromJson(jsonStr, type);
return t;
}

public <T> List<T> getModelList(String jsonStr, Class<T[]> clazz) {
T[] yourClassList = new Gson().fromJson(jsonStr, clazz);
return Arrays.asList(yourClassList);
}
}

我试过了

String s = RESTUtil.doPost(url, formData);
ResponseMessage<ArrayList<CallDetail>> model = Converter.me().getModel(s, ResponseMessage<ArrayList<CallDetail>>.class);

但这是错误的。 s - 它是 Json 字符串。

ResponseMessage<ArrayList<CallDetail>>.class - type(I don't know how pass it)

最佳答案

我创建了一个 SSCE ,这本质上是一个带有解决方案的简短的自包含示例。请注意内联评论。

请注意,我们已将 MyClass 的边界限制为扩展 List 类。

package com.so34985191;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.List;

/**
* Solution for SO question 34985191
*/
public class GsonTest {
public static void main(String args[]) {
Converter converter = new Converter();

List<Integer> sampleList1 = Arrays.asList(new Integer[] {1, 2, 3});
List<Integer> sampleList2 = Arrays.asList(new Integer[] {5, 6, 7});

@SuppressWarnings({"rawtypes"})
MyClass[] test = {
new MyClass<List<Integer>, Integer>(sampleList1),
new MyClass<List<Integer>, Integer>(sampleList2)};

Gson gs = new Gson();
String jsonEncodedString = gs.toJson(test);

// Example where the type is provided to the getModelList method
Type listType = new TypeToken<MyClass<List<Integer>, Integer>[]>() {}.getType();
System.out.println(converter.getModelList(jsonEncodedString, listType));

// Trying with another example.
Type nameType = new TypeToken<Test[]>() {}.getType();
System.out.println(converter.getModelList(
gs.toJson(new Test[] {new Test("Java"), new Test("Scala")}), nameType));
}

/**
*
* Utility class to convert a json encoded strings to respective types.
*/
public static class Converter {
/**
* Converts the encoded json string to respective type depeding on the provided type parameter
* @param jsonStr the string to be decoded.
* @param type the type used for conversion
* @return the decoded list
*/
public <T> List<T> getModelList(String jsonStr, Type type) {
T[] yourClassList = new Gson().fromJson(jsonStr, type);
return Arrays.asList(yourClassList);
}
}

/**
* Container for a list of ids.
* @param <E>
*/
public static class MyClass<T extends List<E>, E> {
private final T ids;

public MyClass(T ids) {
this.ids = ids;
}

public T getId() {
return this.ids;
}

@Override
public String toString() {
return this.ids.toString();
}
}

/**
* A sample wrapper over string.
*/
public static class Test {
private final String name;

public Test(String name) {
this.name = name;
}

public String getName() {
return this.name;
}

@Override
public String toString() {
return this.name;
}
}
}

关于Java 如何将类<ArrayList<T>> 传递给方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34985191/

25 4 0