簡體   English   中英

Json序列化為空

[英]Json Serializing to Null

我對Json的需求不是很大,所以對我來說仍然處於那種令人頭疼的挫敗感。 對於這是騙子,我深表歉意,但我在搜索中發現的所有內容都與其他問題有關。

我正在嘗試使用Walmart API,並且我的數據很好(我在下面附加了一條記錄供參考)。 我能找到的所有現有問題似乎都與傳入的空數據有關。這是關於空數據流出的問題。

問題/疑問:我對API的調用產生了巨大的回報-記錄數為1000(未壓縮為385MB)。 當我反序列化時,我也沒有異常,仍然得到1000條記錄,但是返回列表中的每個屬性為null或0。因此,Json返回了一個泛型列表,該列表具有記錄,並且記錄也不為null,但是所有每個記錄內的值/屬性的值均為null(如果為數字,則為0)。

我在序列化過程中設置了一個斷點,並且可以看到json數據正確輸入。 它只是以null / 0的形式出現。

如果您有任何想法-非常感謝您可以提供的任何幫助。 感謝您抽出寶貴的時間閱讀本文,並感謝您提供的任何幫助。

不良結果:

Item_ID itemId  parentItemId    name    salePrice   upc shortDescription    longDescription brandName   thumbnailImage  mediumImage largeImage  productTrackingUrl  ninetySevenCentShipping standardShipRate    size    color   marketplace shipToStore freeShipToStore productUrl  customerRating  numReviews  customerRatingImage rollBack    bundle  clearance   stock   gender  age freeShippingOver50Dollars   availableOnline
 1  0   0   NULL    0   NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    0   0   NULL    NULL    0   0   0   NULL    NULL    0   NULL    0   0   0   NULL    NULL    NULL    0   0

序列化邏輯:

public API_Json_Special_Feeds.RootObject testExistingJson()
    {
        string json = "";
        //I saved a response to a text file so I don't overload the API with testing. The same problem happens whether or not I load from API or text file.
        using(TextReader tr = new StreamReader(@"C:jsonsingle.txt"))
        {
            json = tr.ReadToEnd();//string
        }
        var serializer = new DataContractJsonSerializer(typeof(API_Json_Special_Feeds.RootObject));

        var ms = new MemoryStream(Encoding.UTF8.GetBytes(json));
        var data = (API_Json_Special_Feeds.RootObject)serializer.ReadObject(ms);
         return data;
    }

樣本Json記錄:

 {"items":[ {
 "itemId" : 12321374,
 "parentItemId" : 12321374,
 "name" : "Castle Tent Loft Bed w/ Slide ; Under Bed Storage, Blue",
 "salePrice" : 369.0,
 "upc" : "081438383996",
 "shortDescription" : "Finished in White powder coat with Blue and Red Microfiber fabric tent",
 "longDescription" : "Fun Castle Tent Bunk Bed with Slide includes a tent over twin bed and a covered hiding place below. The covered hiding place below can also be used as under bed storage. The top of the slide is tented with a tower with peek through, fold down window covers. <ul class="noindent"><li>Finished in White powder coat with Blue and Red Microfiber fabric tent, 100% polyester.</li><li>Uses a standard twin size mattress (sold separately).</li><li>Only use a twin size mattress that is 74-75" long and 37.5-38.5" wide.</li><li>Bunk bed is tested and certified by an independent testing laboratory to meet all minimum requirements of ASTM F1427-07 and U.S. CFR 1213 and 1513.</li><li>Some assembly required. 79-3/4" x 101-1/4" x 92" tall.</li></ul>",
 "brandName" : "Castle",
 "thumbnailImage" : "https://i5.walmartimages.com/asr/a1bac83b-68f2-427e-82ac-97ce1e11b0c6_1.f18ff9fc14c7aca2d9c92c81f4b5c8ba.jpeg?odnHeight=100&odnWidth=100&odnBg=ffffff",
 "mediumImage" : "https://i5.walmartimages.com/asr/a1bac83b-68f2-427e-82ac-97ce1e11b0c6_1.f18ff9fc14c7aca2d9c92c81f4b5c8ba.jpeg?odnHeight=180&odnWidth=180&odnBg=ffffff",
 "largeImage" : "https://i5.walmartimages.com/asr/a1bac83b-68f2-427e-82ac-97ce1e11b0c6_1.f18ff9fc14c7aca2d9c92c81f4b5c8ba.jpeg?odnHeight=450&odnWidth=450&odnBg=ffffff",
 "productTrackingUrl" : "http://linksynergy.walmart.com/fs-bin/click?id=|LSNID|&offerid=223073.7200&type=14&catid=8&subid=0&hid=7200&tmpid=1082&RD_PARM1=http%253A%252F%252Fwww.walmart.com%252Fip%252FCastle-Tent-Loft-Bed-w-Slide--Under-Bed-Storage-Blue%252F12321374%253Faffp1%253D%257Capk%257C%2526affilsrc%253Dapi",
 "ninetySevenCentShipping" : false,
 "standardShipRate" : 0.0,
 "size" : "Twin",
 "color" : "Blue",
 "marketplace" : false,
 "shipToStore" : true,
 "freeShipToStore" : true,
 "productUrl" : "http://c.affil.walmart.com/t/api00rb?l=http%3A%2F%2Fwww.walmart.com%2Fip%2FCastle-Tent-Loft-Bed-w-Slide--Under-Bed-Storage-Blue%2F12321374%3Faffp1%3D%7Capk%7C%26affilsrc%3Dapi%26veh%3Daff%26wmlspartner%3Dreadonlyapi",
 "customerRating" : "4.574",
 "numReviews" : 54,
 "customerRatingImage" : "http://i2.walmartimages.com/i/CustRating/4_6.gif",
 "rollBack" : true,
 "bundle" : true,
 "clearance" : false,
 "stock" : "Available",
 "gender" : "Boys",
 "age" : "Child",
 "freeShippingOver50Dollars" : true,
 "availableOnline" : true
}]
}

Json類別:

public class API_Json_Special_Feeds
{
    [DataContract]
    public class Item
    {
        public int itemId { get; set; }
        public int parentItemId { get; set; }
        public string name { get; set; }
        public double salePrice { get; set; }
        public string upc { get; set; }
        public string shortDescription { get; set; }
        public string longDescription { get; set; }
        public string brandName { get; set; }
        public string thumbnailImage { get; set; }
        public string mediumImage { get; set; }
        public string largeImage { get; set; }
        public string productTrackingUrl { get; set; }
        public bool ninetySevenCentShipping { get; set; }
        public double standardShipRate { get; set; }
        public string size { get; set; }
        public string color { get; set; }
        public bool marketplace { get; set; }
        public bool shipToStore { get; set; }
        public bool freeShipToStore { get; set; }
        public string productUrl { get; set; }
        public string customerRating { get; set; }
        public int numReviews { get; set; }
        public string customerRatingImage { get; set; }
        public bool rollBack { get; set; }
        public bool bundle { get; set; }
        public bool clearance { get; set; }
        public string stock { get; set; }
        public string gender { get; set; }
        public string age { get; set; }
        public bool freeShippingOver50Dollars { get; set; }
        public bool availableOnline { get; set; }
    }
    [DataContract]
    public class RootObject
    {
        [DataMember]
        public List<Item> items { get; set; }
    }
}

您應該將[DataMember]屬性添加到Item類的屬性中。 現在,只有“項目”列表被“檢測到”,並且項目對象被序列化為默認值

第1步:

在此站點中放入JSON,以構建C#POCO模型( http://json2csharp.com/

第2步:

從Newtonsoft獲取Json.Net。

第三步:

RootObject myObject = JsonConvert.DeserializeObject<RootObject>(jsonString);

要簡單地返回另一種方式:

string jsonString = JsonConvert.SerializeObject<RootObject>(myObject);

第四步:

閱讀有關Json.Net的所有內容,因為它在處理.Net中的JSON方面比其他任何東西都要優越。 另外,如果您喜歡冒險,請閱讀動態類型(如果您還沒有的話)。 Json.Net和C#中的動態類型=更少的代碼可以完成相同的操作。

最后一件事。 不需要任何屬性,只需要一個空的構造函數,或者就不需要構造函數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM