2016-02-26 70 views
0

我試圖解析使用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 
    } 
] 

回答

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);

希望這將有助於找出問題。如果你仍然無法找到,讓我知道...我會更努力... :-)

+0

我不明白如何從響應中刪除數組。你可以在這個地方放一些燈光! – AVINASH

0

也許這是給你空,因爲你搞砸了你的註釋......

此外,從所有的方法和字段刪除static,你正試圖使實例變量,而不是類變量。

@SerializedName("product_id") // <---- not right 
public String getName() { // <--- removed "static" 
    return name; 
} 
+0

感謝您的迴應..但我試過後刪除它..但沒有成功 – AVINASH

0

對象屬性是靜態的,你應該告訴GsonBuilder序列化它。

關注此stack瞭解更多信息。

更好的做法是不要把歸檔靜態POJO類:)