我正在尝试使用Gson解析JSON响应文档,但在解析它后给我一个空值
我的JSON响应是一个文档数组
Java代码
//Code to convert the response into JSON
String res = gson.toJson(results);
//Parse the JSON
java.lang.reflect.Type collectionType = new TypeToken<List<Objects.JsonResponse>>() {}.getType();
List<Objects.JsonResponse> resp = gson.fromJson(res, collectionType);
System.out.println(resp.get(2).getName());
JAVA对象
package Objects;
import java.util.List;
import com.google.gson.annotations.SerializedName;
public class JsonResponse {
@SerializedName("product_id")
public static String product_id; //17
@SerializedName("create_date")
public static String create_date; //45
@SerializedName("image_small")
public static String image_small; //85
@SerializedName("image_large")
public static String image_large; //133
@SerializedName("name")
private static String name; //174
@SerializedName("description")
public static String description; //266
@SerializedName("tagline")
public static List<String> tagline;
@SerializedName("category")
public static List<String> category;
@SerializedName("catlevel0")
public static List<String> catlevel0;
@SerializedName("catlevel1")
public static List<String> catlevel1;
@SerializedName("catlevel2")
public static List<String> catlevel2;
@SerializedName("color")
public static List<String> color;
@SerializedName("size")
public static List<String> size;
@SerializedName("_version_")
public static String _version_;
@SerializedName("product_id")
public static String getName() {
return name;
}
public static void setName(String name) {
JsonResponse.name = name;
}
}
要解析的JSON文档是:
[
{
"product_id": "prod3400008",
"create_date": "2011-02-17T00:00:00Z",
"image_small": "/hul_images/small/17_Rexona.jpg",
"image_large": "/hul_images/large/17_Rexona.jpg",
"name": "Small Shell Cluster Loop Earrings",
"description": "Small Shell Cluster Loop Earrings",
"tagline": [
"B1G1 75% Off Jewelry "
],
"category": [
"Earrings"
],
"catlevel0": [
"Accessories"
],
"catlevel1": [
"Jewelry"
],
"catlevel2": [
"Earrings"
],
"color": [
"Clearly Coral",
"Mocha Brown",
"Blue Lagoon",
"Hunter Green",
"Medium Purple"
],
"_version_": 1527034576315089000
}
]
答案 0 :(得分:0)
也许它会给你null,因为你搞砸了你的注释......
此外,从所有方法和字段中删除static
,您正在尝试创建实例变量,而不是类变量。
@SerializedName("product_id") // <---- not right
public String getName() { // <--- removed "static"
return name;
}
答案 1 :(得分:0)
答案 2 :(得分:0)
不是解决方案,更像是提示。我在你的问题中发现了问题。问题出在json数组中。如果删除所有数组而只删除一个对象,则不会出现此问题。我不知道你是否已经意识到这一点。
请尝试以下json
{
"product_id": "prod3400008",
"create_date": "2011-02-17T00:00:00Z",
"image_small": "/hul_images/small/17_Rexona.jpg",
"image_large": "/hul_images/large/17_Rexona.jpg",
"name": "Small Shell Cluster Loop Earrings",
"description": "Small Shell Cluster Loop Earrings",
"_version_": 1527034576315089000
}
我删除了所有数组。
我还删除了类JsonResponse
的静态声明,以及所有成员变量的静态声明。
这是尝试此代码:
Gson gson = new Gson();
String res = gson.toJson(results);
JsonResponse response = gson.fromJson(results, JsonResponse.class);
System.out.println(response.product_id);
System.out.println(response.create_date);
希望这有助于找出问题所在。如果你仍然无法找到答案,请告诉我......我会更加努力......: - )