C# Entity To Json

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# Entity To Json相关的知识,希望对你有一定的参考价值。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Text;
using System.Web.Script.Serialization;

/// <summary>
/// Summary description for Common
/// </summary>
public class Common
{
    private static javascriptSerializer jss;
    public static string DataTableToJson(DataTable dt)
    {
        if (dt == null || dt.Rows.Count == 0)
            return "";

        int length = dt.Rows.Count;

        StringBuilder strbuild = new StringBuilder();
        strbuild.Append("[");//转换成多个model的形式 
        for (int i = 0; i < length; i++)
        {
            strbuild.Append("{");
            for (int j = 0; j < dt.Columns.Count; j++)
            {
                strbuild.Append("\"");
                strbuild.Append(dt.Columns[j].ColumnName);
                strbuild.Append("\":\"");
                strbuild.Append(dt.Rows[i][j].ToString());
                strbuild.Append("\",");
            }
            strbuild.Remove(strbuild.Length - 1, 1);
            strbuild.Append("},");
        }
        strbuild.Remove(strbuild.Length - 1, 1);
        strbuild.Append("]");
        return strbuild.ToString();
    }

    public static string EntityToJson(object obj)
    {
        if (null == jss)
            jss = new JavaScriptSerializer();

        return jss.Serialize(obj);
    }
}

 

以上是关于C# Entity To Json的主要内容,如果未能解决你的问题,请参考以下文章

google vision - 在 c# 中从 google.cloud.vision.v1.entity 注释转换为 json

C# Entity Framework Json 反序列化字符串数组问题

C# DataTable To Json 实现

C# Object to Json to Xml,如何为数组项生成xml元素

.NET 中的 LINQ to JSON

JSON TO C#反序列化[关闭]