logixologist
logixologist

Reputation: 3834

JSON Not Deserializing

This is one of my first ventures into WCF/JSON. I created a WCF Web Service. This is one of my methods. It is how I serialize the datable to JSON.

   public string GetPrayers()
    {
        DataTable myDt = new DataTable();
        myDt = sprocToDT("LoadPrayers");
        string JSONString = string.Empty;
        JSONString = JsonConvert.SerializeObject(myDt, Formatting.None);
        return JSONString;
    }

This returns a nice JSON Dataset:

{"GetPrayersResult":"[{\"prayerid\":2,\"prayer\":\"Please pray for my dog Rusty. He has cancer :(\",\"prayerCategory\":\"General\",\"prayerDate\":\"2017-06-10T21:24:16.1\",\"handle\":\"GuruJee\",\"country\":\"USA\"},{\"prayerid\":1,\"prayer\":\"Help Me I need a appendectomy STAT\",\"prayerCategory\":\"Sports\",\"prayerDate\":\"2017-04-10T20:30:39.77\",\"handle\":\"GuruJee\",\"country\":\"USA\"}]"}

When I go to deserialize it I get all nulls. Here is the classes I created:

 public class PrayUpPrayers
        {
            public string prayer { get; set; }
            public string prayerid { get; set; }
            public string prayerCategory { get; set; }
            public string prayerCategoryID { get; set; }
            public string prayerDate { get; set; }
            public string handle { get; set; }
            public string country { get; set; }  
        }

        public class ThePrayer
        {
        public PrayUpPrayers prayers { get; set; }
        }


    }

This is how I am retrieving the JSON:

void getData()
        {
            var request = HttpWebRequest.Create(string.Format(@"URLGoesHere"));
            request.ContentType = "application/json";
            request.Method = "GET";

            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                if (response.StatusCode != HttpStatusCode.OK)
                    Console.Out.WriteLine("Error fetching data. Server returned status code: {0}", response.StatusCode);
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    var content = reader.ReadToEnd();
                    string foo = content.ToString();

                    var testing = JsonConvert.DeserializeObject<prayupapp.ModelClasses.PrayUpPrayers>(foo,
               new JsonSerializerSettings
               {
                   NullValueHandling = NullValueHandling.Ignore
               });

Testing is always null? Is the issue that I am serializing it wrong, could it be the class structure, or is it related to how I am deserializing it. One important note: I checked my JSON on one of these JSONClassesFromC# sites and it only returns the GetPrayersResult as the only class item. Ignoring completely my entire structure.

Upvotes: 2

Views: 4593

Answers (2)

Mohammad
Mohammad

Reputation: 2764

that's simple. you should deserilze the output twice. try this:

var output= DeserializeObject<string>(foo);

var testing = JsonConvert.DeserializeObject<prayupapp.ModelClasses.PrayUpPrayers>(output,
               new JsonSerializerSettings
               {
                   NullValueHandling = NullValueHandling.Ignore
               });

Upvotes: 1

Guy
Guy

Reputation: 50949

You didn't provide the code for sprocToDT, but it should create ThePrayer object witch should contain list of PrayUpPrayers

public class ThePrayer
{
    public List<PrayUpPrayers> prayers { get; set; }
}

And then you should deserialize ThePrayer object, not PrayUpPrayers.

For example

PrayUpPrayers prayUpPrayers1 = new PrayUpPrayers
{
    prayer = "Please pray for my dog Rusty. He has cancer",
    prayerid = "2",
    prayerCategory = "General",
    prayerDate = "2017-06-10T21:24:16.1",
    handle = "GuruJee",
    country = "USA"
};

PrayUpPrayers prayUpPrayers2 = new PrayUpPrayers
{
    prayer = "Help Me I need a appendectomy STAT",
    prayerid = "1",
    prayerCategory = "Sports",
    prayerDate = "2017-04-10T20:30:39.77",
    handle = "GuruJee",
    country = "USA"
};

ThePrayer thePrayer = new ThePrayer
{
    prayers = new List<PrayUpPrayers>
    {
        prayUpPrayers1, prayUpPrayers2
    }
};

myDt in your code should be the same as thePrayer instance in my code.

JSONString = JsonConvert.SerializeObject(myDt, Formatting.None);

will provide Json that looks like

"{\"prayers\":[{\"prayer\":\"Please pray for my dog Rusty. He has cancer\",\"prayerid\":\"2\",\"prayerCategory\":\"General\",\"prayerCategoryID\":null,\"prayerDate\":\"2017-06-10T21:24:16.1\",\"handle\":\"GuruJee\",\"country\":\"USA\"},{\"prayer\":\"Help Me I need a appendectomy STAT\",\"prayerid\":\"1\",\"prayerCategory\":\"Sports\",\"prayerCategoryID\":null,\"prayerDate\":\"2017-04-10T20:30:39.77\",\"handle\":\"GuruJee\",\"country\":\"USA\"}]}"

And deserialize will look like

var testing = JsonConvert.DeserializeObject<prayupapp.ModelClasses.ThePrayer>(foo,
    new JsonSerializerSettings
    {
        NullValueHandling = NullValueHandling.Ignore
    });

Upvotes: 1

Related Questions