Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

handling JSON when null values appear

Posted on 2015-02-04
2
Medium Priority
?
194 Views
Last Modified: 2015-02-04
I'm building up a .NET service that consumes JSON from a 3rd party service and I'm using JSON.net to parse it out into it's own class. This works perfectly fine when all the fields are present, but if any of them return a blank or null value, it errors out. Below is a segment of the JSON being returned:

“Status”:
{
    "status": "Shipped",
    "tracking_number": [
        {
            "sku": "3346",
            "trackingNumber": "273436470000723                                   "
        },
        {
            "sku": "4186",
            "trackingNumber": "273436470000723                                   "
        }
} ]

Open in new window


there are times when the tracking number will be blank, it'll look like:

“Status”:
{
    "status": "Shipped",
    "tracking_number": []
}

Open in new window


and here's my class definition:

    public class CCG_ReceivedOrders
    {
        public string order_id                          { get; set; }
        public CCG_ReceivedOrderStatus status           { get; set; }
    }

    public class CCG_ReceivedOrderStatus
    {
        public string status                            { get; set; }
        public CCG_TrackingNumbers tracking_number      { get; set; }
    }

    public class CCG_TrackingNumbers
    {
        public int sku                                  { get; set; }
        public string tracking_number                   { get; set; }
    }

Open in new window


if I comment out CCG_TrackingNumbers.tracking_Number, it works fine. if that field contains a value, it works fine, so my only thought is that it is not handling the null value properly. The exact error message I'm getting is:

Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'CCG_Data_Objects.CCG_TrackingNumbers' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
Path '[0].status.tracking_number', line 1, position 922.

Open in new window


any help is greatly appreciated!
0
Comment
Question by:Big Monty
  • 2
2 Comments
 
LVL 34

Author Comment

by:Big Monty
ID: 40589110
I found the NullValueHandling attribute that json.net supports, and changed my parsing code to

return JsonConvert.DeserializeObject<List<T>>(jsonData,new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore});

but i still get the same error. is the json data format bad?
0
 
LVL 34

Accepted Solution

by:
Big Monty earned 0 total points
ID: 40589183
figured out the solution, I needed to return a list of tracking numbers, so I changed my code to:

    public class CCG_ReceivedOrderStatus
    {
        public string status                             { get; set; }
        public List<CCG_TrackingNumbers> tracking_number { get; set; }
    }

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Loops Section Overview

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question