JSON不反序列化

时间:2017-06-11 05:21:20

标签: c# json wcf serialization deserialization

这是我第一次进入WCF / JSON。我创建了一个WCF Web服务。这是我的方法之一。这是我将数据序列化为JSON的方式。

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

这会返回一个不错的JSON数据集:

  

{“GetPrayersResult”:“[{\”“prayid \”:2,\“祈祷\”:\“请为我祈祷   狗生锈。他患有癌症   (\”,\ “prayerCategory \”:\ “常规\”,\ “prayerDate \”:\ “2017-06-10T21:24:16.1 \”,\ “处理\”:\ “GuruJee \”,\ “国家\”:\ “美国\”},{\ “prayerid \”:1,\ “祈祷\”:\“帮助   我需要阑尾切除术   STAT \ “\ ”prayerCategory \“:\ ”体育\“,\ ”prayerDate \“:\ ”2017-04-10T20:30:39.77 \“,\ ”处理\“:\ ”GuruJee \“,\”国\ “:\” 美国\ “}]”}

当我去反序列化时,我得到所有空值。这是我创建的类:

 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; }
        }


    }

这就是我检索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
               });

测试始终为空?问题是我序列化错误,可能是类结构,还是与我反序列化有关。一个重要的注意事项:我在其中一个JSONClassesFromC#站点上检查了我的JSON,它只返回GetPrayersResult作为唯一的类项。完全忽略我的整个结构。

2 个答案:

答案 0 :(得分:1)

您没有提供sprocToDT的代码,但应创建ThePrayer对象,其中应包含PrayUpPrayers的列表

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

然后你应该反序列化ThePrayer对象,而不是PrayUpPrayers

例如

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应与我代码中的thePrayer实例相同。

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

将提供类似

的Json
  

&#34; {\&#34;祈祷\&#34;:[{\&#34;祈祷\&#34;:\&#34;请为我的狗生锈祈祷。他有   癌症\&#34; \&#34; prayerid \&#34;:\&#34; 2 \&#34; \&#34; prayerCategory \&#34;:\&#34;通用\ &#34; \&#34; prayerCategoryID \&#34;:空,\&#34; prayerDate \&#34;:\&#34; 2017-06-10T21:24:16.1 \&#34; ,\&#34;处理\&#34;:\&#34; GuruJee \&#34; \&#34;国家\&#34;:\&#34;美国\&#34;}, {\&#34;祈祷\&#34;:\&#34;帮助   我需要阑尾切除术   STAT \&#34; \&#34; prayerid \&#34;:\&#34; 1 \&#34; \&#34; prayerCategory \&#34;:\&#34;体育\ &#34; \&#34; prayerCategoryID \&#34;:空,\&#34; prayerDate \&#34;:\&#34; 2017-04-10T20:30:39.77 \&#34; ,\&#34;处理\&#34;:\&#34; GuruJee \&#34; \&#34;国家\&#34;:\&#34;美国\&#34;}] }&#34;

反序列化看起来像

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

答案 1 :(得分:1)

这很简单。你应该两次输出输出。试试这个:

var output= DeserializeObject<string>(foo);

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