【问题标题】:JSON.net format output like I need我需要的 JSON.net 格式输出
【发布时间】:2015-12-26 20:22:37
【问题描述】:

嘿,我写了以下 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 to class online generator 我得到以下类:

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

我正在通过 SQL 服务器查询为这些 json 字符串填写所需的数据:

 try
 {
     string connetionString = @"Data Source=(localdb)\v11.0;Initial Catalog=stantecUsers;Integrated Security=True";
     string sql = "SELECT * " +
                  "FROM [stantecUsers].[dbo].[users] AS stantecUsers " +
                  "INNER JOIN [stantecUsers].[dbo].[usersData] AS stantecUserData " +
                  "ON stantecUsers.link2Data = stantecUserData.link2Data" +
                  "WHERE stantecUsers.phoneNum = '" + phoneNum + "'" +
                  "ORDER BY [stantecUsers].ID ASC;";
     SqlConnection connection;
     SqlCommand command;
     connection = new SqlConnection(connetionString);

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

        while (read.Read())
        {
           Debug.WriteLine(read["firstName"].ToString());
        }

        read.Close();
        command.Dispose();
        connection.Close();
     }
     catch (Exception ex)
     {
         Debug.WriteLine("Can not open connection ! " + ex.Message);
     }
   }
   catch (SqlException e)
   {
        return e.Message;
   }
}

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

【问题讨论】:

    标签: c# sql json wcf json.net


    【解决方案1】:

    您需要创建一个与您想要获取的 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();
    }
    

    【讨论】:

    • 很好的例子。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-03
    • 1970-01-01
    相关资源
    最近更新 更多