簡體   English   中英

如何自定義json feed C#

[英]how to customize json feed C#

我試圖在C#中開發json feed。 但無法達到我的要求

下面我添加了我的代碼

public string ConvertDataTabletoString()
    {
        DataTable dt = new DataTable();

        SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ToString());
        SqlCommand command = new SqlCommand("select * from table4", connection);
        SqlDataAdapter da = new SqlDataAdapter(command);
        da.Fill(dt);
            JavaScriptSerializer serializer = new  JavaScriptSerializer();

        List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
        Dictionary<string, object> row;
        foreach (DataRow dr in dt.Rows)
        {
            row = new Dictionary<string, object>();
            foreach (DataColumn col in dt.Columns)
            {
                row.Add(col.ColumnName, dr[col]);
            }
            rows.Add(row);
        }
        return serializer.Serialize(rows);
    }

我得到了低於輸出。 來自數據庫的所有值。

[

    {
        "username":"raja",
        "empid":"45"
    },
    {
        "username":"z",
        "empid":"z"
    },
    {
        "username":"sdfs",
        "empid":"dfsdfsd"
    },
    {
        "username":"df",
        "empid":"d"
    }

]

但是我對格式以下的要求如何更改格式以下的輸出

 {
            "contacts":[
            {
            "username":"raja",
            "employee":{
            "empid":"45"
            }
            }
            ]
            }

我想知道如何在json feed中添加“聯系人”

只需使用一個匿名對象進行序列化

return serializer.Serialize(new { contacts = rows } ); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM