在 C# 中使用 .NET 3.5 将数据集转换为 JSON

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/355833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 00:19:03  来源:igfitidea点击:

Converting a dataset to JSON using .NET 3.5 in C#

c#.netjsondataset.net-3.5

提问by

I have been searching for a simple way to convert a dataset from a PostgreSQLdatabase to JSON for use in a project that I am building.

我一直在寻找一种简单的方法来将数据集从PostgreSQL数据库转换为 JSON,以便在我正在构建的项目中使用。

This is my first time using JSON, and I have found it really tricky to find a simple way of doing this. I have been using a StringBuilder at the moment to create a JSON string from the information in the dataset, but I have heard that it is possible to do this very simply with .NET3.5 using the System.Runtime.Serializationnamespace, though I have yet to find a simple article or blog on how this is done! What is the easiest way to do this?

这是我第一次使用 JSON,我发现找到一种简单的方法来做到这一点真的很棘手。目前我一直在使用 StringBuilder 从数据集中的信息创建一个 JSON 字符串,但我听说使用命名空间的.NET3.5 可以非常简单地做到这一点System.Runtime.Serialization,尽管我还没有找到一个简单的关于如何做到这一点的文章或博客!什么是最简单的方法来做到这一点?

回答by finnsson

Use Newtonsofts Json.Netand check out DataTable JSON Serialization in JSON.NET and JavaScriptSerializerwhere it's used to create a DataSet-to-JSON converter.

使用 Newtonsofts Json.Net并查看JSON.NET 和 JavaScriptSerializer中的DataTable JSON 序列化,它用于创建数据集到 JSON 转换器。

回答by gimel

Maybe you've heard about the system.runtime.serialization.jsonnamespace in the newly announced .NET Framework 4.0.

也许您听说过新发布的.NET Framework 4.0 中system.runtime.serialization.json命名空间。

回答by DineshHona

public static string GetJSONString(DataTable Dt)
{
    string[] StrDc = new string[Dt.Columns.Count];
    string HeadStr = string.Empty;

    for (int i = 0; i < Dt.Columns.Count; i++)
    {
        StrDc[i] = Dt.Columns[i].Caption;
        HeadStr += "\"" + StrDc[i] + "\" : \"" + StrDc[i] + i.ToString() + "?" + "\",";
    }

    HeadStr = HeadStr.Substring(0, HeadStr.Length - 1);

    StringBuilder Sb = new StringBuilder();
    Sb.Append("{\"" + Dt.TableName + "\" : [");

    for (int i = 0; i < Dt.Rows.Count; i++)
    {
        string TempStr = HeadStr;
        Sb.Append("{");

        for (int j = 0; j < Dt.Columns.Count; j++)
        {
            TempStr = TempStr.Replace(Dt.Columns[j] + j.ToString() + "?", Dt.Rows[i][j].ToString());
        }
        Sb.Append(TempStr + "},");
    }

    Sb = new StringBuilder(Sb.ToString().Substring(0, Sb.ToString().Length - 1));
    Sb.Append("]}");

    return Sb.ToString();
}

回答by dvdmn

For others looking at this topic:

对于正在查看此主题的其他人:

Here is the simplest way to convert a dataset to a JSON array as json_encode(PHP) does with ASP.Net:

这是将数据集转换为 JSON 数组的最简单方法,就像json_encode( PHP) 对 ASP.Net 所做的那样:

using Newtonsoft.Json;

public static string ds2json(DataSet ds) {
    return JsonConvert.SerializeObject(ds, Formatting.Indented);
}