从JSON字符串获取对象时遇到一些问题.
我上了课 Product
public class Product {
private String mBarcode;
private String mName;
private String mPrice;
public Product(String barcode, String name, String price) {
mBarcode = barcode;
mName = name;
mPrice = price;
}
public int getBarcode() {
return Integer.parseInt(mBarcode);
}
public String getName() {
return mName;
}
public double getPrice() {
return Double.parseDouble(mPrice);
}
}
Run Code Online (Sandbox Code Playgroud)
从我的服务器,我得到一个ArrayList<Product>JSON字符串表示.例如:
[{"mBarcode":"123","mName":"Apfel","mPrice":"2.7"},
{"mBarcode":"456","mName":"Pfirsich","mPrice":"1.1111"},
{"mBarcode":"89325982","mName":"Birne","mPrice":"1.5555"}]
Run Code Online (Sandbox Code Playgroud)
此String生成如下:
public static <T> String arrayToString(ArrayList<T> list) {
Gson g = new Gson();
return g.toJson(list);
}
Run Code Online (Sandbox Code Playgroud)
为了让我的对象回来,我使用这个函数:
public …Run Code Online (Sandbox Code Playgroud) 我正在使用相当多的不可变集合,我很好奇如何使用Gson对它们进行反序列化.由于没有人回答我自己找到了解决方案,我正在简化问题并提出自己的答案.
我有两个问题:
Deserializer为所有人写一个单一的工作ImmutableList<XXX>?ImmutableList<XXX>?我正在编写一些代码,用于从网站中检索资源.它看起来像这样:
public Collection<Project> getProjects() {
String json = getJsonData(methods.get(Project.class)); //Gets a json list, ie [1, 2, 3, 4]
Gson gson = new Gson();
Type collectionType = new TypeToken<Collection<Project>>() {}.getType();
return gson.fromJson(json, collectionType);
}
Run Code Online (Sandbox Code Playgroud)
所以我很自然地尝试使用Java泛型来抽象它.
/*
* Deserialize a json list and return a collection of the given type.
*
* Example usage: getData(AccountQuota.class) -> Collection<AccountQuota>
*/
@SuppressWarnings("unchecked")
public <T> Collection<T> getData(Class<T> cls) {
String json = getJsonData(methods.get(cls)); //Gets a json list, ie [1, 2, 3, 4]
Gson gson = new …Run Code Online (Sandbox Code Playgroud) 我想创建一个解析器来使用 Gson 解析 JSON。
首先 IParser.java
public interface IParser<T> {
public T parse(String json);
}
Run Code Online (Sandbox Code Playgroud)
第二 Parser.java
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
public class Parser<T> implements IParser<T> {
@Override
public T parse(String json) {
GsonBuilder builder = new GsonBuilder();
Gson gson = builder.enableComplexMapKeySerialization().create();
MyJson<T> jsonParsed = gson.fromJson(json, new TypeToken<MyJson<T>>() {
}.getType());
System.out.println(jsonParsed.toString());
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
第三 MyJson.java
public class MyJson<T> {
private int status;
private T data;
public int getStatus() {
return status;
}
public void setStatus(int status) …Run Code Online (Sandbox Code Playgroud) 我正在尝试在Android应用程序中使用REST服务,但在序列化ArrayList时遇到问题:
我的RESTful服务返回:

我的Android应用程序收到:

我正在使用Gson库来序列化我的对象,当然问题与它有关.从这个意义上讲,我尝试按如下方式制作Serializer:
public class MySerializer<E>
implements JsonSerializer<Collection<E>>, JsonDeserializer<Collection<E>> {
@Override
public Collection<E> deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context) throws JsonParseException {
Type type = new TypeToken<List<E>>(){}.getType();
return context.deserialize(json, type);
}
@Override
public JsonElement serialize(Collection<E> src, Type typeOfSrc,
JsonSerializationContext context) {
Type type = new TypeToken<List<E>>(){}.getType();
return context.serialize(src, type);
}
}
Run Code Online (Sandbox Code Playgroud)
注册apapter(registerTypeAdapter(ArrayList.class, new MySerializer<Object>())),但这不起作用......
有人可以帮帮我吗?
对于json映射,我使用以下方法:
public static <T> T mapJsonToObject(String json, T dtoClass) throws Exception {
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(json, new TypeReference<RestResponse<UserDto>>() {
});
}
Run Code Online (Sandbox Code Playgroud)
和UserDto看起来像这样:
@JsonIgnoreProperties(ignoreUnknown = true)
public class UserDto {
@JsonProperty("items")
private List<User> userList;
public List<User> getUserList() {
return userList;
}
public void setUserList(List<User> userList) {
this.userList = userList;
}
}
Run Code Online (Sandbox Code Playgroud)
我想改进这种映射方法,而不必附加到UserDto类,而是将其替换为通用类。
可能吗?如何?
谢谢。