在服务器端绑定JSON值失败

时间:2018-11-30 08:54:10

标签: c# asp.net json list

我想在我的C#代码中使用json值。所以我写了下面的代码

public static void WriteToIEMService(string jsonValue)
    {
        string TimeFormatText = DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss");
        string strFileCreationDate = DateTime.Now.ToString("dd/MM/yyyy");
        string strFileName = ConfigurationManager.AppSettings["IEM_SERVICEFILE"].ToString();

        try
        {            
           using (StreamWriter sw = File.CreateText(ConfigurationManager.AppSettings["LogFileDirectory"].ToString() + strFileName + "_" + strFileCreationDate + ".txt"))
            {
                List<MasterServiceResponse> records = JsonConvert.DeserializeObject<List<MasterServiceResponse>>(jsonValue);

                sw.WriteLine("IEM Service started and send data to IEM below");

                foreach (MasterServiceResponse record in records)
               {
                   sw.WriteLine(record.SapId);
               }
            }
        }
        catch (Exception)
        {

            throw;
        }
    }

但是我得到了错误

  

无法将当前JSON对象(例如{“ name”:“ value”})反序列化为类型'System.Collections.Generic.List`1 [IPColoBilling_BKP.App_Code.MasterServiceResponse]',因为该类型需要JSON数组(例如[1,2,3])正确反序列化。

请帮助

编辑

我的json值如下

{"SiteData":[{"SAPId":"I-UW-SRPR-ENB-I001","SiteRFEIDate":"03-11-2014","SiteRFSDate":"03-11-2014","ID_OD":"ID","ID_ODchangeDate":"04-11-2018","NoofRRHBase":"0","RRHBaseChangeEffectiveDate":"","No_Of_Tenancy":"3","TenancyChangeEffectiveDate":"03-11-2014","SiteStatus":"Active","SiteDropDate":""}]}

更新

public class MasterServiceResponse
{
    public string SapId { get; set; }
    public string AcknowledgementID { get; set; }
    public string FlagResponse { get; set; }
    public string ResponseMessage { get; set; }
    public string GisStatus { get; set; }
    public string GisSendDate { get; set; }
    public string SiteRFEIDate { get; set; }
    public string SiteRFSDate { get; set; }
    public string SiteRRHDate { get; set; }
    public string NoofRRHBase { get; set; }
}

4 个答案:

答案 0 :(得分:3)

更新:Panagiotis Kanavos所说,您不是在创建Root对象,因此必须重新构建模型。

不知道您的模型是什么样子,但是此错误消息表明您希望JSON数组的位置提供给json对象,

下面是您应该使用的模型

public class SiteData
{
    public string SAPId { get; set; }
    public string SiteRFEIDate { get; set; }
    public string SiteRFSDate { get; set; }
    public string ID_OD { get; set; }
    public string ID_ODchangeDate { get; set; }
    public string NoofRRHBase { get; set; }
    public string RRHBaseChangeEffectiveDate { get; set; }
    public string No_Of_Tenancy { get; set; }
    public string TenancyChangeEffectiveDate { get; set; }
    public string SiteStatus { get; set; }
    public string SiteDropDate { get; set; }
}

public class RootObject
{
    public List<SiteData> SiteData { get; set; }
}

您还必须更改反序列化代码

例如

RootObject records = JsonConvert.DeserializeObject<RootObject>(jsonValue);

foreach (SiteData record in records.SiteData)
{
   sw.WriteLine(record.SapId);
}

答案 1 :(得分:0)

如错误所述,您不能反序列化JSON对象以列出。所以代替:

List<MasterServiceResponse> records = JsonConvert.DeserializeObject<List<MasterServiceResponse>>(jsonValue);

您应该更新以下内容:

MyObject obj = JsonConvert.DeserializeObject<MyObject>(jsonValue);

答案 2 :(得分:0)

该异常消息告诉您JSON字符串包含单个对象,但是您尝试将其反序列化为List<MasterServiceResponse>集合。

您应该创建一个容纳List<MasterServiceResponse>的类:

public class SiteData
{
    public List<MasterServiceResponse> MasterServiceResponse { get; set; }
}

然后您应该替换此行:

List<MasterServiceResponse> records = JsonConvert.DeserializeObject<List<MasterServiceResponse>>(jsonValue);

返回这样的单个对象:

SiteData records = JsonConvert.DeserializeObject<SiteData>(jsonValue);

参考:

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1

答案 3 :(得分:0)

由于您的JSON以具有数组值的键"SiteData"开头,因此您需要创建一个帮助程序类,以将该结构(例如class SiteResponse)与名为SiteData的属性进行匹配输入List<MasterServiceResponse>

代码:

public class SiteResponse
{
    public List<MasterServiceResponse> SiteData { get; set; }
}

private static void TestJson()
{
    var jsonValue = "{\"SiteData\":[{\"SAPId\":\"I-UW-SRPR-ENB-I001\",\"SiteRFEIDate\":\"03-11-2014\",\"SiteRFSDate\":\"03-11-2014\",\"ID_OD\":\"ID\",\"ID_ODchangeDate\":\"04-11-2018\",\"NoofRRHBase\":\"0\",\"RRHBaseChangeEffectiveDate\":\"\",\"No_Of_Tenancy\":\"3\",\"TenancyChangeEffectiveDate\":\"03-11-2014\",\"SiteStatus\":\"Active\",\"SiteDropDate\":\"\"}]}";

    var siteResponse = JsonConvert.DeserializeObject<SiteResponse>(jsonValue);
    List<MasterServiceResponse> records = siteResponse.SiteData;
}

此处的演示工作:
https://dotnetfiddle.net/wzg8AK