Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在我的服务器端绑定JSON值失败_C#_Asp.net_Json_List - Fatal编程技术网

C# 在我的服务器端绑定JSON值失败

C# 在我的服务器端绑定JSON值失败,c#,asp.net,json,list,C#,Asp.net,Json,List,我想在我的c#code中使用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");

我想在我的c#code中使用
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;
        }
    }
更新

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

更新:Panagiotis Kanavos指出,您没有创建根对象,因此必须重新构造模型

不知道您的模型是什么样子的,但此错误消息表示您将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; }
}
公共类站点数据
{
公共字符串SAPId{get;set;}
公共字符串SiteRFEIDate{get;set;}
公共字符串SiteRFSDate{get;set;}
公共字符串ID_OD{get;set;}
公共字符串ID_ODchangeDate{get;set;}
公共字符串NoofRRHBase{get;set;}
公共字符串RRHBaseChangeEffectiveDate{get;set;}
公共字符串No_Of_{get;set;}
公共字符串TenancyChangeEffectiveDate{get;set;}
公共字符串SiteStatus{get;set;}
公共字符串SiteDropDate{get;set;}
}
公共类根对象
{
公共列表站点数据{get;set;}
}
您还必须更改反序列化代码

例如

rootobjectrecords=JsonConvert.DeserializeObject(jsonValue);
foreach(records.SiteData中的SiteData记录)
{
sw.WriteLine(record.SapId);
}

更新:Panagiotis Kanavos指出,您没有创建根对象,因此必须重新构造模型

不知道您的模型是什么样子的,但此错误消息表示您将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; }
}
公共类站点数据
{
公共字符串SAPId{get;set;}
公共字符串SiteRFEIDate{get;set;}
公共字符串SiteRFSDate{get;set;}
公共字符串ID_OD{get;set;}
公共字符串ID_ODchangeDate{get;set;}
公共字符串NoofRRHBase{get;set;}
公共字符串RRHBaseChangeEffectiveDate{get;set;}
公共字符串No_Of_{get;set;}
公共字符串TenancyChangeEffectiveDate{get;set;}
公共字符串SiteStatus{get;set;}
公共字符串SiteDropDate{get;set;}
}
公共类根对象
{
公共列表站点数据{get;set;}
}
您还必须更改反序列化代码

例如

rootobjectrecords=JsonConvert.DeserializeObject(jsonValue);
foreach(records.SiteData中的SiteData记录)
{
sw.WriteLine(record.SapId);
}

正如错误所述,您无法将JSON对象反序列化为列表。因此,不是:

List<MasterServiceResponse> records = JsonConvert.DeserializeObject<List<MasterServiceResponse>>(jsonValue);
List records=JsonConvert.DeserializeObject(jsonValue);
您应该有如下更新:

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

正如错误所述,您无法将JSON对象反序列化为列表。因此,不是:

List<MasterServiceResponse> records = JsonConvert.DeserializeObject<List<MasterServiceResponse>>(jsonValue);
List records=JsonConvert.DeserializeObject(jsonValue);
您应该有如下更新:

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

异常消息告诉您JSON字符串包含单个对象,但您试图将其反序列化到
列表
集合中

您应该创建一个类来保存
列表

公共类站点数据
{
公共列表MasterServiceResponse{get;set;}
}
那么您应该替换这一行:

List<MasterServiceResponse> records = JsonConvert.DeserializeObject<List<MasterServiceResponse>>(jsonValue);
List records=JsonConvert.DeserializeObject(jsonValue);
要像这样返回单个对象,请执行以下操作:

SiteData records = JsonConvert.DeserializeObject<SiteData>(jsonValue);
sitedatarecords=JsonConvert.DeserializeObject(jsonValue);
参考:


异常消息告诉您,JSON字符串包含单个对象,但您试图将其反序列化到
列表
集合中

您应该创建一个类来保存
列表

公共类站点数据
{
公共列表MasterServiceResponse{get;set;}
}
那么您应该替换这一行:

List<MasterServiceResponse> records = JsonConvert.DeserializeObject<List<MasterServiceResponse>>(jsonValue);
List records=JsonConvert.DeserializeObject(jsonValue);
要像这样返回单个对象,请执行以下操作:

SiteData records = JsonConvert.DeserializeObject<SiteData>(jsonValue);
sitedatarecords=JsonConvert.DeserializeObject(jsonValue);
参考:


因为JSON以具有数组值的键“SiteData”开头,所以需要创建一个帮助器类,以将该结构(例如
类SiteResponse
)与类型为
List
的名为
SiteData
的属性相匹配

代码:

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;
}
公共类站点响应
{
公共列表站点数据{get;set;}
}
私有静态void TestJson()
{
目前,该国的“网站外交部”将作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为。“2014年11月11-2014,”,”作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为“:\”活动“,\”SiteDropDate\“:\“\”}]}”;
var siteResponse=JsonConvert.DeserializeObject(jsonValue);
列表记录=siteResponse.SiteData;
}
此处的工作演示:

因为您的JSON以一个具有数组值的键
“SiteData”
开始,所以您需要创建一个帮助器类,以将该结构(例如
类sitereponse
)与类型为
List
的名为
SiteData
的属性相匹配

代码:

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;
}
公共类站点响应
{
公共列表站点数据{get;set;}
}
私有静态void TestJson()
{
目前,该国的“网站外交部”将作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为。“2014年11月11-2014,”,”作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为作为“:\”活动“,\”SiteDropDate\“:\“\”}]}”;
var siteResponse=JsonConvert.DeserializeObject(jsonValue);
列表记录=siteResponse.SiteData;
}
工作演示h