Shannon Holsinger Shannon Holsinger - 3 years ago 169
JSON Question

Json Serializing to Null

I haven't had much need for Json, so it's still at that head-banging degree of frustration for me. I apologize if this is a dupe, but everything I found in searches had to do with different issues.

I'm trying to use the Walmart API, and my data is coming through fine (I've attached a single record below for reference). All the existing questions I can find seem to deal with null data coming in. This is about null data going out.

Problem/Question: My call to the API results in a huge return - in the 1,000's of records (385MB uncompressed). When I deserialize, I get no exception and I still get 1000's of records, but each property in the returned list is null or 0. So, Json returns a list of generics and the list has records and the records are not null, but all of the values/properties inside each records are null (or 0 if numeric).

I've set a breakpoint in the serialization process, and I can see the json data coming in correctly. It's just coming out as null/0.

If you have any ideas - I'd really appreciate any help you can provide. Thank you for your time in reading this, and in advance for any assistance you can provide.

Undesired Result:

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

Serialization Logic:

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;

Sample Json Record:

{"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" : "",
"mediumImage" : "",
"largeImage" : "",
"productTrackingUrl" : "|LSNID|&offerid=223073.7200&type=14&catid=8&subid=0&hid=7200&tmpid=1082&",
"ninetySevenCentShipping" : false,
"standardShipRate" : 0.0,
"size" : "Twin",
"color" : "Blue",
"marketplace" : false,
"shipToStore" : true,
"freeShipToStore" : true,
"productUrl" : "",
"customerRating" : "4.574",
"numReviews" : 54,
"customerRatingImage" : "",
"rollBack" : true,
"bundle" : true,
"clearance" : false,
"stock" : "Available",
"gender" : "Boys",
"age" : "Child",
"freeShippingOver50Dollars" : true,
"availableOnline" : true

Json Class:

public class API_Json_Special_Feeds
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; }
public class RootObject
public List<Item> items { get; set; }

Answer Source

You should add [DataMember] attribute to properties in Item class. Right now only Items list is "detected", and Item obcject are serialized with default values

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download