如何自定义JSON提要C# [英] how to customize json feed C#

查看:130
本文介绍了如何自定义JSON提要C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图向发展JSON提要在C#。但无法得到它我的要求

下面我增加了我的code

 公共字符串ConvertDataTabletoString()
    {
        数据表DT =新的DataTable();

        SqlConnection的连接=新的SqlConnection(ConfigurationManager.ConnectionStrings [DBConnection的]的ToString());
        SqlCommand的命令=新的SqlCommand(选择从表4 *,连接);
        SqlDataAdapter的DA =新的SqlDataAdapter(命令);
        da.Fill(DT);
            JavaScriptSerializer串行=新JavaScriptSerializer();

        名单<字典<字符串,对象>>行=新的名单,其中,字典<字符串,对象>>();
        字典<字符串,对象>行;
        的foreach(在dt.Rows的DataRow博士)
        {
            行=新字典<字符串,对象>();
            的foreach(COL的DataColumn在dt.Columns)
            {
                row.Add(col.ColumnName,博士[COL]);
            }
            rows.Add(行);
        }
        返回serializer.Serialize(行);
    }
 

我得到了下面的输出。 所有值从数据库正在添加。

  [

    {
        用户名:拉贾
        EMPID:45
    },
    {
        用户名:Z,
        EMPID:Z
    },
    {
        用户名:SDFS
        EMPID:dfsdfsd
    },
    {
        用户名:东风,
        EMPID:D
    }

]
 

但低于format.how我需要改变如下格式我的输出

  {
            人脉:
            {
            用户名:拉贾
            雇员:{
            EMPID:45
            }
            }
            ]
            }
 

解决方案
  

我想知道如何在JSON饲料中添加联系人

只需使用一个匿名对象序列化

 返回serializer.Serialize(新{触点=行});
 

i tried to develope json feed in C# . but unable get it my requirement

below i have added my code

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

i got a below output . all values comming from database.

[

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

]

but my requirement below format.how to change my output below format

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

解决方案

i want to know how to add "contacts" in json feed

Just use an anonymous object to serialize

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

这篇关于如何自定义JSON提要C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆