Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.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.net格式输出,如我所需_C#_Sql_Json_Wcf_Json.net - Fatal编程技术网

C# JSON.net格式输出,如我所需

C# JSON.net格式输出,如我所需,c#,sql,json,wcf,json.net,C#,Sql,Json,Wcf,Json.net,嘿,我有下面的JSON输出,我写的就像我需要它被JSON.net输出一样: {"DATA": { "firstName": "Bill", "lastName": "Gates", "emailAddress": "billgates@microsoft.com", "phoneNum": "5552349856", "image": "https://upload.wikimedia.org/wikipedia/commons/1/19/Bill_Gates_June_20

嘿,我有下面的JSON输出,我写的就像我需要它被JSON.net输出一样:

{"DATA": {
  "firstName": "Bill",
  "lastName": "Gates",
  "emailAddress": "billgates@microsoft.com",
  "phoneNum": "5552349856",
  "image": "https://upload.wikimedia.org/wikipedia/commons/1/19/Bill_Gates_June_2015.jpg",
  "title": "CEO",
  "clocked": [
      {"date": "12-13-2015 17:00:00", "type": "OUT"},
      {"date": "12-13-2015 13:00:00", "type": "IN"},
      {"date": "12-12-2015 14:30:00", "type": "OUT"},
      {"date": "12-12-2015 10:00:00", "type": "IN"},
      {"date": "12-11-2015 17:00:00", "type": "OUT"},
      {"date": "12-11-2015 13:00:00", "type": "IN"},
      {"date": "12-10-2015 10:30:00", "type": "OUT"},
      {"date": "12-10-2015 08:00:00", "type": "IN"},
      {"date": "12-09-2015 13:45:00", "type": "OUT"},
      {"date": "12-09-2015 09:00:00", "type": "IN"},
      {"date": "12-08-2015 12:30:00", "type": "OUT"},
      {"date": "12-08-2015 10:00:00", "type": "IN"},
      {"date": "12-07-2015 13:45:00", "type": "OUT"},
      {"date": "12-07-2015 08:30:00", "type": "IN"},
      {"date": "12-06-2015 12:10:00", "type": "OUT"},
      {"date": "12-06-2015 09:40:00", "type": "IN"},
      {"date": "12-05-2015 18:00:00", "type": "OUT"},
      {"date": "12-05-2015 14:10:00", "type": "IN"},
      {"date": "12-04-2015 12:30:00", "type": "OUT"},
      {"date": "12-04-2015 08:00:00", "type": "IN"}
  ]
}
}
使用C#JSON对在线生成器进行分类,我得到以下类:

public class Clocked
{
    public string date { get; set; }
    public string type { get; set; }
}

public class DATA
{
    public string firstName { get; set; }
    public string lastName { get; set; }
    public string emailAddress { get; set; }
    public string phoneNum { get; set; }
    public string image { get; set; }
    public string title { get; set; }
    public List<Clocked> clocked { get; set; }
}

public class RootObject
{
    public DATA DATA { get; set; }
}

那么,我如何让JSON.net输出看起来像我的模型一样呢?

您需要创建一个与您想要获得的JSON结构匹配的对象,然后可以序列化该对象以获得最终的JSON。代码可能与此类似:

string finalJSON = "";

try
{
   connection.Open();
   command = new SqlCommand(sql, connection);
   SqlDataReader read = command.ExecuteReader();

   // Create a new object that matches the structure of the JSON file.
   var root = new RootObject
   {
       DATA = new DATA { clocked = new List<Clocked>() }
   };

   while (read.Read())
   {
      root.DATA.firstName = read["firstName"].ToString();
      root.DATA.lastName = read["lastName"].ToString();
      // Continue with the other attributes...
      root.DATA.clocked.Add(new Clocked {date = read["date"].ToString(), type = read["type"].ToString() });
   }

   // Serialize the object using JSON.Net.
   finalJSON = JsonConvert.SerializeObject(root);

   read.Close();
   command.Dispose();
   connection.Close();
}
string finalJSON=”“;
尝试
{
connection.Open();
command=newsqlcommand(sql,connection);
SqlDataReader read=command.ExecuteReader();
//创建与JSON文件结构匹配的新对象。
var root=新的根对象
{
数据=新数据{时钟=新列表()}
};
while(read.read())
{
root.DATA.firstName=读取[“firstName”].ToString();
root.DATA.lastName=读取[“lastName”].ToString();
//继续使用其他属性。。。
root.DATA.clocked.Add(新的时钟{date=read[“date”].ToString(),type=read[“type”].ToString()});
}
//使用JSON.Net序列化对象。
finalJSON=JsonConvert.SerializeObject(根);
read.Close();
command.Dispose();
connection.Close();
}

您需要创建一个与要获取的JSON结构匹配的对象,然后可以序列化该对象以获取最终的JSON。代码可能与此类似:

string finalJSON = "";

try
{
   connection.Open();
   command = new SqlCommand(sql, connection);
   SqlDataReader read = command.ExecuteReader();

   // Create a new object that matches the structure of the JSON file.
   var root = new RootObject
   {
       DATA = new DATA { clocked = new List<Clocked>() }
   };

   while (read.Read())
   {
      root.DATA.firstName = read["firstName"].ToString();
      root.DATA.lastName = read["lastName"].ToString();
      // Continue with the other attributes...
      root.DATA.clocked.Add(new Clocked {date = read["date"].ToString(), type = read["type"].ToString() });
   }

   // Serialize the object using JSON.Net.
   finalJSON = JsonConvert.SerializeObject(root);

   read.Close();
   command.Dispose();
   connection.Close();
}
string finalJSON=”“;
尝试
{
connection.Open();
command=newsqlcommand(sql,connection);
SqlDataReader read=command.ExecuteReader();
//创建与JSON文件结构匹配的新对象。
var root=新的根对象
{
数据=新数据{时钟=新列表()}
};
while(read.read())
{
root.DATA.firstName=读取[“firstName”].ToString();
root.DATA.lastName=读取[“lastName”].ToString();
//继续使用其他属性。。。
root.DATA.clocked.Add(新的时钟{date=read[“date”].ToString(),type=read[“type”].ToString()});
}
//使用JSON.Net序列化对象。
finalJSON=JsonConvert.SerializeObject(根);
read.Close();
command.Dispose();
connection.Close();
}

很好的例子。谢谢很好的例子。谢谢