JSON到POJO使用Gson

时间:2015-11-30 15:33:01

标签: java json

我有一个带有JSON响应主体的REST GET调用如下(相当长):

{"data":[{"length_class_id":"1","rating":0,"points":"0","discounts":[],"reviews":{"review_total":"0"},"price":"3330.0000","jan":"","options":[],"model":"iPhone 6s 64GB Gold ","id":"40","sku":"product 11","product_description":{"1":{"meta_description":"Apple iPhone 6s 64GB gold colour smartphone with 4.70-inch 750x1334 Retina HD display alongside 2GB RAM and 12-megapixel rear camera.\r\nWith 3D Touch, Live Photos, 7000 Series aluminium, the A9 chip, advanced cameras,  Retina HD display and so much more th","meta_title":"iphone | mobile ","meta_keyword":"iPhone  6s  64GB","name":"iPhone 6s 64GB Gold ","description":"<p class=\"MsoNormal\"><span style=\"font-size:10.0pt;font-family:Times;display:\r\nnone;mso-hide:all\"><o:p>&nbsp;<\/o:p><\/span><\/p><p class=\"MsoNormal\"><span style=\"font-size:10.0pt;font-family:Times;display:\r\nnone;mso-hide:all\"><o:p>&nbsp;<\/o:p><\/span><\/p><h1 style=\"line-height: 1;\"><span style=\"color: rgb(239, 198, 49); font-family: 'Helvetica Neue';\">iPhone 6s<span style=\"font-weight: bold;\">&nbsp;<\/span><\/span><\/h1><h2 style=\"line-height: 1;\"><span style=\"color: rgb(239, 198, 49); font-family: 'Helvetica Neue';\">&nbsp;<\/span><\/h2><p class=\"MsoNormal\" style=\"line-height: 1;\"><span style=\"font-family: 'Helvetica Neue';\"><span style=\"font-weight: bold; color: rgb(239, 198, 49);\">Features<\/span><br><\/span><\/p><p class=\"MsoNormal\" style=\"line-height: 1;\"><span style=\"font-family: 'Helvetica Neue';\">4.7\" Retina HD display<\/span><span style=\"font-family: 'Helvetica Neue'; line-height: 1;\">&nbsp;<\/span><\/p><p class=\"MsoNormal\" style=\"line-height: 1;\"><span style=\"font-family: 'Helvetica Neue';\">Display with 1334-by-750 resolution<\/span><\/p><p class=\"MsoNormal\" style=\"line-height: 1;\"><span style=\"font-family: 'Helvetica Neue';\">Colour : Gold<\/span><\/p><p class=\"MsoNormal\" style=\"line-height: 1;\"><span style=\"font-family: 'Helvetica Neue';\">Capacity : 64GB<\/span><\/p><p class=\"MsoNormal\" style=\"line-height: 1;\"><o:p style=\"font-family: 'Helvetica Neue';\">&nbsp;<\/o:p><\/p><p class=\"MsoNormal\" style=\"line-height: 1;\"><span style=\"color: rgb(239, 198, 49); font-family: 'Helvetica Neue';\"><span style=\"mso-spacerun: yes\">&nbsp;<\/span><span style=\"font-weight: bold;\">3D Touch<\/span><\/span><\/p><p class=\"MsoNormal\" style=\"line-height: 1;\"><span style=\"font-family: 'Helvetica Neue';\">When you use 3D Touch, your iPhone responds with subtle\r\ntaps. So not only will you see what <\/span><\/p><p class=\"MsoNormal\" style=\"line-height: 1;\"><span style=\"font-family: 'Helvetica Neue';\">a press can do - you\u2019ll feel it<\/span><\/p><p class=\"MsoNormal\" style=\"line-height: 1;\"><o:p style=\"font-family: 'Helvetica Neue';\">&nbsp;<\/o:p><\/p><p class=\"MsoNormal\" style=\"line-height: 1;\"><span style=\"font-weight: bold; color: rgb(239, 198, 49); font-family: 'Helvetica Neue';\">A9 Chip<\/span><\/p><p class=\"MsoNormal\" style=\"line-height: 1;\"><span style=\"font-family: 'Helvetica Neue';\">A9 chip with 64-bit architecture and with M9 motion\r\ncoprocessor<\/span><\/p><p class=\"MsoNormal\" style=\"line-height: 1;\"><o:p style=\"font-family: 'Helvetica Neue';\">&nbsp;<\/o:p><\/p><p class=\"MsoNormal\" style=\"line-height: 1;\"><span style=\"font-weight: bold; color: rgb(239, 198, 49); font-family: 'Helvetica Neue';\">iOS 9<\/span><\/p><p class=\"MsoNormal\" style=\"line-height: 1;\"><span style=\"font-family: 'Helvetica Neue';\">iOS 9 brings together an elegant and intuitive interface,\r\npowerful features, and robust security. <\/span><\/p><p class=\"MsoNormal\" style=\"line-height: 1;\"><span style=\"font-family: 'Helvetica Neue';\">Designed to work as beautifully as it\r\nlooks so you can enjoy everything you do on a device that <\/span><\/p><p class=\"MsoNormal\" style=\"line-height: 1;\"><span style=\"font-family: 'Helvetica Neue';\">does everything<\/span><\/p><p class=\"MsoNormal\" style=\"line-height: 1;\"><o:p style=\"font-family: 'Helvetica Neue';\">&nbsp;<\/o:p><\/p><p class=\"MsoNormal\" style=\"line-height: 1;\"><span style=\"font-weight: bold; color: rgb(239, 198, 49); font-family: 'Helvetica Neue';\">12-megapixel iSight Camera<\/span><\/p><h5 style=\"line-height: 1;\">12-megapixel iSight camera with&nbsp;ƒ/2.2 aperture,&nbsp;Focus Pixels, True Tone\r\nflash, and optical <\/h5><h5 style=\"line-height: 1;\">image stabilization<\/h5><p class=\"MsoNormal\" style=\"line-height: 1;\"><o:p style=\"font-family: 'Helvetica Neue';\">&nbsp;<\/o:p><\/p><p class=\"MsoNormal\" style=\"line-height: 1;\"><span style=\"color: rgb(239, 198, 49); font-weight: bold; font-family: 'Helvetica Neue';\">FaceTime HD Camera<\/span><\/p><p class=\"MsoNormal\" style=\"line-height: 1;\"><span style=\"font-family: 'Helvetica Neue';\">5MP photos, 720p HD video with FaceTime video calling over\r\nWi Fi or cellular<\/span><\/p><p class=\"MsoNormal\" style=\"line-height: 1;\"><o:p style=\"font-family: 'Helvetica Neue';\">&nbsp;<\/o:p><\/p><p class=\"MsoNormal\" style=\"line-height: 1;\"><span style=\"font-weight: bold; color: rgb(239, 198, 49); font-family: 'Helvetica Neue';\">Touch ID<\/span><\/p><p class=\"MsoNormal\" style=\"line-height: 1;\"><span style=\"font-family: 'Helvetica Neue';\">Fingerprint sensor built into the Home button<\/span><\/p><p class=\"MsoNormal\" style=\"line-height: 1;\"><o:p style=\"font-family: 'Helvetica Neue';\">&nbsp;<\/o:p><\/p><p class=\"MsoNormal\" style=\"line-height: 1;\"><span style=\"color: rgb(239, 198, 49); font-family: 'Helvetica Neue';\">&nbsp;<span style=\"font-weight: bold;\">4K video Recording<\/span><\/span><\/p><p class=\"MsoNormal\" style=\"line-height: 1;\"><span style=\"font-family: 'Helvetica Neue';\">1080p HD video recording at 30fps or 60fps and slo-mo video\r\nsupport for 1080p at 120 fps and <\/span><\/p><p class=\"MsoNormal\" style=\"line-height: 1;\"><span style=\"font-family: 'Helvetica Neue';\">720p at 240 fps<\/span><\/p><p class=\"MsoNormal\" style=\"line-height: 1;\"><o:p style=\"font-family: 'Helvetica Neue';\">&nbsp;<\/o:p><\/p><p class=\"MsoNormal\" style=\"line-height: 1;\"><span style=\"font-weight: bold; color: rgb(239, 198, 49); font-family: 'Helvetica Neue';\">GPS with Navigation Capability<\/span><\/p><p class=\"MsoNormal\" style=\"line-height: 1;\"><span style=\"font-family: 'Helvetica Neue';\">Get your real-time location on maps, driving directions, and\r\nmore.<\/span><\/p><p class=\"MsoNormal\" style=\"line-height: 1;\"><o:p style=\"font-family: 'Helvetica Neue';\">&nbsp;<\/o:p><\/p><p class=\"MsoNormal\" style=\"line-height: 1;\"><span style=\"color: rgb(239, 198, 49); font-family: 'Helvetica Neue';\"><span style=\"mso-spacerun: yes\">&nbsp;<\/span><span style=\"font-weight: bold;\">Siri - Your intelligent assistant<\/span><\/span><\/p><p class=\"MsoNormal\" style=\"line-height: 1;\"><span style=\"font-family: 'Helvetica Neue';\">Ask Siri to make calls, send texts, set reminders, and more.\r\nJust talk the way you talk. Siri <\/span><\/p><p class=\"MsoNormal\" style=\"line-height: 1;\"><span style=\"font-family: 'Helvetica Neue';\">understands what you say and knows what you\r\nmean.<\/span><\/p><p class=\"MsoNormal\" style=\"line-height: 1;\"><o:p style=\"font-family: 'Helvetica Neue';\">&nbsp;<\/o:p><\/p><p class=\"MsoNormal\" style=\"line-height: 1;\"><span style=\"font-weight: bold; color: rgb(239, 198, 49); font-family: 'Helvetica Neue';\">Bluetooth 4.2<\/span><\/p><p class=\"MsoNormal\" style=\"line-height: 1;\"><span style=\"font-family: 'Helvetica Neue';\">Bluetooth 4.2<\/span><\/p><p class=\"MsoNormal\" style=\"line-height: 1;\"><o:p style=\"font-family: 'Helvetica Neue';\">&nbsp;<\/o:p><\/p><p class=\"MsoNormal\" style=\"line-height: 1;\"><span style=\"color: rgb(239, 198, 49);\"><span style=\"font-weight: bold; font-family: 'Helvetica Neue';\">LTE Capable<\/span><\/span><\/p><p class=\"MsoNormal\" style=\"line-height: 1;\"><span style=\"font-family: 'Helvetica Neue';\">LTE Capable.<\/span><\/p><p class=\"MsoNormal\" style=\"line-height: 1;\"><o:p style=\"font-family: 'Helvetica Neue';\">&nbsp;<\/o:p><\/p><p class=\"MsoNormal\" style=\"line-height: 1;\"><span style=\"font-weight: bold; color: rgb(239, 198, 49); font-family: 'Helvetica Neue';\">Wi-Fi Calling<\/span><\/p><p class=\"MsoNormal\" style=\"line-height: 1;\">\r\n\r\n\r\n\r\n\r\n\r\n\r\n<!--[if gte mso 9]><xml>\r\n <o:OfficeDocumentSettings>\r\n  <o:AllowPNG><\/o:AllowPNG>\r\n <\/o:OfficeDocumentSettings>\r\n<\/xml><![endif]--><!--[if gte mso 9]><xml>\r\n <w:WordDocument>\r\n  <w:Zoom>0<\/w:Zoom>\r\n  <w:TrackMoves>false<\/w:TrackMoves>\r\n  <w:TrackFormatting><\/w:TrackFormatting>\r\n  <w:PunctuationKerning><\/w:PunctuationKerning>\r\n  <w:DrawingGridHorizontalSpacing>18 pt<\/w:DrawingGridHorizontalSpacing>\r\n  <w:DrawingGridVerticalSpacing>18 pt<\/w:DrawingGridVerticalSpacing>\r\n  <w:DisplayHorizontalDrawingGridEvery>0<\/w:DisplayHorizontalDrawingGridEvery>\r\n  <w:DisplayVerticalDrawingGridEvery>0<\/w:DisplayVerticalDrawingGridEvery>\r\n  <w:ValidateAgainstSchemas><\/w:ValidateAgainstSchemas>\r\n  <w:SaveIfXMLInvalid>false<\/w:SaveIfXMLInvalid>\r\n  <w:IgnoreMixedContent>false<\/w:IgnoreMixedContent>\r\n  <w:AlwaysShowPlaceholderText>false<\/w:AlwaysShowPlaceholderText>\r\n  <w:Compatibility>\r\n   <w:BreakWrappedTables><\/w:BreakWrappedTables>\r\n   <w:DontGrowAutofit><\/w:DontGrowAutofit>\r\n   <w:DontAutofitConstrainedTables><\/w:DontAutofitConstrainedTables>\r\n   <w:DontVertAlignInTxbx><\/w:DontVertAlignInTxbx>\r\n  <\/w:Compatibility>\r\n <\/w:WordDocument>\r\n<\/xml><![endif]--><!--[if gte mso 9]><xml>\r\n <w:LatentStyles DefLockedState=\"false\" LatentStyleCount=\"276\">\r\n <\/w:LatentStyles>\r\n<\/xml><![endif]-->\r\n\r\n<!--[if gte mso 10]>\r\n<style>\r\n /* Style Definitions */\r\ntable.MsoNormalTable\r\n\t{mso-style-name:\"Table Normal\";\r\n\tmso-tstyle-rowband-size:0;\r\n\tmso-tstyle-colband-size:0;\r\n\tmso-style-noshow:yes;\r\n\tmso-style-parent:\"\";\r\n\tmso-padding-alt:0cm 5.4pt 0cm 5.4pt;\r\n\tmso-para-margin:0cm;\r\n\tmso-para-margin-bottom:.0001pt;\r\n\tmso-pagination:widow-orphan;\r\n\tfont-size:12.0pt;\r\n\tfont-family:\"Times New Roman\";\r\n\tmso-ascii-font-family:Cambria;\r\n\tmso-ascii-theme-font:minor-latin;\r\n\tmso-hansi-font-family:Cambria;\r\n\tmso-hansi-theme-font:minor-latin;}\r\n<\/style>\r\n<![endif]-->\r\n\r\n\r\n\r\n<!--StartFragment-->\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n<!--EndFragment--><\/p><p class=\"MsoNormal\" style=\"line-height: 1;\"><span style=\"font-family: 'Helvetica Neue';\">Call normally, or use Wi-Fi when you don't have cellular\r\nreception.<\/span><\/p><h6 style=\"line-height: 1;\"><span style=\"font-family: 'Helvetica Neue';\"><br><\/span><\/h6><h5 style=\"font-family: 'Open Sans', sans-serif; line-height: 1; color: rgb(68, 68, 68); margin-top: 10px; margin-bottom: 10px;\"><span style=\"color: rgb(239, 198, 49); font-weight: bold; line-height: 1; font-family: 'Helvetica Neue';\">Power &amp; Battery&nbsp;<\/span><\/h5><h5 style=\"margin-top: 10px; margin-bottom: 10px;\"><p class=\"MsoNormal\" style=\"\">Talk time : Up to 14 hours on 3G<br><\/p><\/h5><h5 style=\"margin-top: 10px; margin-bottom: 10px;\">Internet use : Up to 10 hours on 3G,&nbsp;up to 10 hours on 4G LTE, up to 11 hours on Wi-Fi<\/h5><h5 style=\"margin-top: 10px; margin-bottom: 10px;\">HD video playback : Up to 11 hours<\/h5><h5 style=\"margin-top: 10px; margin-bottom: 10px;\">Audio playback : Up to 50 hours<\/h5><h5 style=\"margin-top: 10px; margin-bottom: 10px;\">Standby time : Up to 10 days<\/h5><h5 style=\"line-height: 1;\"><span style=\"color: rgb(239, 198, 49); line-height: 1; font-family: 'Helvetica Neue';\"><br><\/span><\/h5><h5 style=\"line-height: 1;\"><span style=\"font-family: 'Helvetica Neue';\"><span style=\"color: rgb(239, 198, 49); line-height: 1; font-weight: bold;\">In the Box<\/span><br><\/span><\/h5><h5><p class=\"MsoNormal\" style=\"\">iPhone with iOS 9<br><\/p><p class=\"MsoNormal\">Apple EarPods with Remote and Mic<br><\/p><p class=\"MsoNormal\">Lightning to USB Cable<br><\/p><p class=\"MsoNormal\">USB Power Adapter<br><\/p><p class=\"MsoNormal\">Documentation<br><\/p>\r\n\r\n<!--EndFragment--><\/h5><h5 style=\"line-height: 1;\"><span style=\"font-family: 'Helvetica Neue';\"><br><\/span><\/h5><h5><br><\/h5><h5><br><\/h5><h5><br><\/h5><h5><br><\/h5><h5><br><\/h5><h5><br><\/h5><h5><br><\/h5><h5><br><\/h5><h5><br><\/h5><h5><br><\/h5><h5><br><\/h5><h5><br><\/h5><h5 style=\"margin-bottom: 0.5em; padding: 0px 10px 0px 0px; color: rgb(51, 51, 51); font-size: 18px; line-height: 26.1px;\">\r\n\r\n\r\n\r\n\r\n\r\n\r\n<!--[if gte mso 9]><xml>\r\n <o:OfficeDocumentSettings>\r\n  <o:AllowPNG><\/o:AllowPNG>\r\n <\/o:OfficeDocumentSettings>\r\n<\/xml><![endif]--><!--[if gte mso 9]><xml>\r\n <w:WordDocument>\r\n  <w:Zoom>0<\/w:Zoom>\r\n  <w:TrackMoves>false<\/w:TrackMoves>\r\n  <w:TrackFormatting><\/w:TrackFormatting>\r\n  <w:PunctuationKerning><\/w:PunctuationKerning>\r\n  <w:DrawingGridHorizontalSpacing>18 pt<\/w:DrawingGridHorizontalSpacing>\r\n  <w:DrawingGridVerticalSpacing>18 pt<\/w:DrawingGridVerticalSpacing>\r\n  <w:DisplayHorizontalDrawingGridEvery>0<\/w:DisplayHorizontalDrawingGridEvery>\r\n  <w:DisplayVerticalDrawingGridEvery>0<\/w:DisplayVerticalDrawingGridEvery>\r\n  <w:ValidateAgainstSchemas><\/w:ValidateAgainstSchemas>\r\n  <w:SaveIfXMLInvalid>false<\/w:SaveIfXMLInvalid>\r\n  <w:IgnoreMixedContent>false<\/w:IgnoreMixedContent>\r\n  <w:AlwaysShowPlaceholderText>false<\/w:AlwaysShowPlaceholderText>\r\n  <w:Compatibility>\r\n   <w:BreakWrappedTables><\/w:BreakWrappedTables>\r\n   <w:DontGrowAutofit><\/w:DontGrowAutofit>\r\n   <w:DontAutofitConstrainedTables><\/w:DontAutofitConstrainedTables>\r\n   <w:DontVertAlignInTxbx><\/w:DontVertAlignInTxbx>\r\n  <\/w:Compatibility>\r\n <\/w:WordDocument>\r\n<\/xml><![endif]--><!--[if gte mso 9]><xml>\r\n <w:LatentStyles DefLockedState=\"false\" LatentStyleCount=\"276\">\r\n <\/w:LatentStyles>\r\n<\/xml><![endif]-->\r\n\r\n<!--[if gte mso 10]>\r\n<style>\r\n /* Style Definitions */\r\ntable.MsoNormalTable\r\n\t{mso-style-name:\"Table Normal\";\r\n\tmso-tstyle-rowband-size:0;\r\n\tmso-tstyle-colband-size:0;\r\n\tmso-style-noshow:yes;\r\n\tmso-style-parent:\"\";\r\n\tmso-padding-alt:0cm 5.4pt 0cm 5.4pt;\r\n\tmso-para-margin:0cm;\r\n\tmso-para-margin-bottom:.0001pt;\r\n\tmso-pagination:widow-orphan;\r\n\tfont-size:12.0pt;\r\n\tfont-family:\"Times New Roman\";\r\n\tmso-ascii-font-family:Cambria;\r\n\tmso-ascii-theme-font:minor-latin;\r\n\tmso-hansi-font-family:Cambria;\r\n\tmso-hansi-theme-font:minor-latin;}\r\n<\/style>\r\n<![endif]-->\r\n\r\n\r\n\r\n<!--StartFragment-->\r\n\r\n<p class=\"MsoNormal\"><span style=\"font-size:10.0pt;font-family:Times;display:\r\nnone;mso-hide:all\"><o:p>&nbsp;<\/o:p><\/span><\/p>\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n<!--EndFragment--><\/h5>\r\n","language_id":"1","tag":"apple "}},"keyword":"iPhone","height":"0.00000000","image":"https://my.mygoldenduck.com/image/cache/catalog/demo/_iphone-gold-angle-1-200x200.jpg","images":["https://my.mygoldenduck.com/image/cache/catalog/demo/_iphone-gold-angle-2-200x200.jpg","https://my.mygoldenduck.com/image/cache/catalog/demo/_iphone-gold-angle-1-200x200.jpg","https://my.mygoldenduck.com/image/cache/catalog/demo/kuala_lumpur-200x200.jpg","https://my.mygoldenduck.com/image/cache/catalog/demo/iPhone_6s_3-200x200.jpg"],"manufacturer_id":"8","stock_status":"In Stock","product_attributes":[],"subtract":"1","upc":"","weight":"10.00000000","mpn":"","date_added":"2009-02-03 21:07:12","weight_class_id":"1","status":"1","price_formated":"RM3,330.00","isbn":"","length_class":"cm","currency_code":"MYR","manufacturer":"Apple","ean":"","viewed":"395","sort_order":"0","reward":null,"quantity":"970","tax_class_id":"0","length":"0.00000000","currency_value":"1.00000000","seo_h1":"","special":[],"date_modified":"2015-11-29 23:36:30","weight_class":"kg","width":"0.00000000","location":"","date_available":"2015-02-03","category":{"68":[{"meta_description":"","category_id":"68","meta_title":"Apple Mobile Devices ","meta_keyword":"","name":"Apple","description":"&lt;p&gt;&lt;br&gt;&lt;/p&gt;","language_id":"1","sort_order":"0"}],"60":[{"meta_description":"","category_id":"60","meta_title":"Mobile Devices ","meta_keyword":"","name":"Mobile Devices &amp; Tablets","description":"&lt;p&gt;Mobile Devices &amp;amp; Tablets&lt;/p&gt;","language_id":"1","sort_order":"2"}]},"minimum":"1","currency_id":"4"}],"success":true}

和我的通讯员pojo类正在关注(从http://www.jsonschema2pojo.org/生成):Products.java

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

@Generated("org.jsonschema2pojo")
public class Products {

    @SerializedName("data")
    @Expose
    private List<Datum> data = new ArrayList<Datum>();
    @SerializedName("success")
    @Expose
    private Boolean success;

    // getter and setter following...

}

和... Datum.java

public class Datum {

    @SerializedName("length_class_id")
    @Expose
    private String lengthClassId;
    @SerializedName("rating")
    @Expose
    private Integer rating;
    @SerializedName("points")
    @Expose
    private String points;
    @SerializedName("discounts")
    @Expose
    private List<Object> discounts = new ArrayList<Object>();
    @SerializedName("reviews")
    @Expose
    private Reviews reviews;
    @SerializedName("price")
    @Expose
    private String price;
    @SerializedName("jan")
    @Expose
    private String jan;
    @SerializedName("options")
    @Expose
    private List<Object> options = new ArrayList<Object>();
    @SerializedName("model")
    @Expose
    private String model;
    @SerializedName("id")
    @Expose
    private String id;
    @SerializedName("sku")
    @Expose
    private String sku;
    @SerializedName("product_description")
    @Expose
    private ProductDescription productDescription;
    @SerializedName("keyword")
    @Expose
    private Object keyword;
    @SerializedName("height")
    @Expose
    private String height;
    @SerializedName("image")
    @Expose
    private String image;
    @SerializedName("images")
    @Expose
    private List<String> images = new ArrayList<String>();
    @SerializedName("manufacturer_id")
    @Expose
    private Object manufacturerId;
    @SerializedName("stock_status")
    @Expose
    private String stockStatus;
    @SerializedName("product_attributes")
    @Expose
    private List<Object> productAttributes = new ArrayList<Object>();
    @SerializedName("subtract")
    @Expose
    private String subtract;
    @SerializedName("upc")
    @Expose
    private String upc;
    @SerializedName("weight")
    @Expose
    private String weight;
    @SerializedName("mpn")
    @Expose
    private String mpn;
    @SerializedName("date_added")
    @Expose
    private String dateAdded;
    @SerializedName("weight_class_id")
    @Expose
    private String weightClassId;
    @SerializedName("status")
    @Expose
    private String status;
    @SerializedName("price_formated")
    @Expose
    private String priceFormated;
    @SerializedName("isbn")
    @Expose
    private String isbn;
    @SerializedName("length_class")
    @Expose
    private String lengthClass;
    @SerializedName("currency_code")
    @Expose
    private String currencyCode;
    @SerializedName("manufacturer")
    @Expose
    private Object manufacturer;
    @SerializedName("ean")
    @Expose
    private String ean;
    @SerializedName("viewed")
    @Expose
    private String viewed;
    @SerializedName("sort_order")
    @Expose
    private String sortOrder;
    @SerializedName("reward")
    @Expose
    private Object reward;
    @SerializedName("quantity")
    @Expose
    private String quantity;
    @SerializedName("tax_class_id")
    @Expose
    private String taxClassId;
    @SerializedName("length")
    @Expose
    private String length;
    @SerializedName("currency_value")
    @Expose
    private String currencyValue;
    @SerializedName("seo_h1")
    @Expose
    private String seoH1;
    @SerializedName("special")
    @Expose
    private List<Special> special = new ArrayList<Special>();
    @SerializedName("date_modified")
    @Expose
    private String dateModified;
    @SerializedName("weight_class")
    @Expose
    private String weightClass;
    @SerializedName("width")
    @Expose
    private String width;
    @SerializedName("location")
    @Expose
    private String location;
    @SerializedName("date_available")
    @Expose
    private String dateAvailable;
    @SerializedName("category")
    @Expose
    private Category category;
    @SerializedName("minimum")
    @Expose
    private String minimum;
    @SerializedName("currency_id")
    @Expose
    private String currencyId;

   // getter and setter following...

}

当我试图将json解析为class时,我的错误如下:

SEVERE: null
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 206022 path $.data[49].category
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:200)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:103)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:196)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40)
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:81)
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:60)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:103)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:196)
    at com.google.gson.Gson.fromJson(Gson.java:810)
    at com.google.gson.Gson.fromJson(Gson.java:775)
    at com.google.gson.Gson.fromJson(Gson.java:724)
    at com.google.gson.Gson.fromJson(Gson.java:696)
    at testproject.UnirestTest.testOne(UnirestTest.java:38)
    at testproject.UnirestTest.main(UnirestTest.java:27)
Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 206022 path $.data[49].category
    at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:387)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:189)
    ... 13 more

这就是我的代码执行解析的方式:

String json = "" + jsonResponse.getBody();
            Products products = new Gson().fromJson(json, Products.class);

编辑:

Datum.java中的类别字段更改为:

private Category[] category;

但后来出现了这个错误:

Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 16353 path $.data[0].category

怎么了?

由于

0 个答案:

没有答案