我試圖解析使用GSON而是一個JSON響應文件的解析之後它給我一個空值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
}
]
我不明白如何從響應中刪除數組。你可以在這個地方放一些燈光! – AVINASH