You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在C#(.NET应用)中将JSON转为换行分隔JSON导入BigQuery?

没问题!要把标准JSON数组转换成BigQuery支持的换行分隔JSON(NDJSON),在C#/.NET里其实很简单,我给你几种实用的方案,你可以根据项目情况选择:

方案1:使用Newtonsoft.Json(Json.NET)

这是.NET生态中最常用的JSON处理库,适合所有.NET版本。

首先安装NuGet包:

Install-Package Newtonsoft.Json

然后用以下代码实现转换:

using Newtonsoft.Json;
using System.Collections.Generic;
using System.IO;

// 定义对应JSON结构的实体类(强类型更安全,避免动态类型的潜在问题)
public class BookingWindow
{
    public string DaysRange { get; set; }
    public int BookingNumber { get; set; }
}

public class DashboardData
{
    public int DashboardCategoryId { get; set; }
    public List<BookingWindow> BookingWindows { get; set; }
}

public static class NdJsonConverter
{
    // 从文件读取JSON并生成NDJSON文件
    public static void ConvertToNdjsonFile(string inputJsonPath, string outputNdjsonPath)
    {
        string inputJson = File.ReadAllText(inputJsonPath);
        List<DashboardData> dataList = JsonConvert.DeserializeObject<List<DashboardData>>(inputJson);
        
        using (StreamWriter writer = new StreamWriter(outputNdjsonPath))
        {
            foreach (var item in dataList)
            {
                // 序列化单个对象时关闭缩进,确保每行是一个完整的单行JSON
                string singleObjectJson = JsonConvert.SerializeObject(item, Formatting.None);
                writer.WriteLine(singleObjectJson);
            }
        }
    }

    // 直接处理JSON字符串并返回NDJSON字符串
    public static string ConvertJsonArrayToNdjson(string inputJson)
    {
        List<DashboardData> dataList = JsonConvert.DeserializeObject<List<DashboardData>>(inputJson);
        List<string> ndjsonLines = new List<string>();
        
        foreach (var item in dataList)
        {
            ndjsonLines.Add(JsonConvert.SerializeObject(item, Formatting.None));
        }
        
        return string.Join(Environment.NewLine, ndjsonLines);
    }
}
方案2:使用System.Text.Json(.NET原生库)

如果你用的是.NET Core 3.0+或.NET 5+,可以直接用官方自带的JSON库,无需额外安装包:

using System.Text.Json;
using System.Collections.Generic;
using System.IO;

// 复用上面的BookingWindow和DashboardData实体类
public static class NdJsonConverter
{
    public static void ConvertToNdjsonFile(string inputJsonPath, string outputNdjsonPath)
    {
        string inputJson = File.ReadAllText(inputJsonPath);
        List<DashboardData> dataList = JsonSerializer.Deserialize<List<DashboardData>>(inputJson);
        
        // 配置序列化选项:关闭缩进
        var serializeOptions = new JsonSerializerOptions { WriteIndented = false };
        
        using (StreamWriter writer = new StreamWriter(outputNdjsonPath))
        {
            foreach (var item in dataList)
            {
                string singleObjectJson = JsonSerializer.Serialize(item, serializeOptions);
                writer.WriteLine(singleObjectJson);
            }
        }
    }

    public static string ConvertJsonArrayToNdjson(string inputJson)
    {
        List<DashboardData> dataList = JsonSerializer.Deserialize<List<DashboardData>>(inputJson);
        var serializeOptions = new JsonSerializerOptions { WriteIndented = false };
        List<string> ndjsonLines = new List<string>();
        
        foreach (var item in dataList)
        {
            ndjsonLines.Add(JsonSerializer.Serialize(item, serializeOptions));
        }
        
        return string.Join(Environment.NewLine, ndjsonLines);
    }
}
方案3:动态处理(无需实体类)

如果你的JSON结构不固定,或者不想定义实体类,可以用动态解析的方式:

using Newtonsoft.Json.Linq; // 需安装Newtonsoft.Json

public static string ConvertJsonArrayToNdjsonDynamic(string inputJson)
{
    JArray jsonArray = JArray.Parse(inputJson);
    List<string> ndjsonLines = new List<string>();
    
    foreach (JToken token in jsonArray)
    {
        // 直接将数组元素转为单行JSON字符串
        ndjsonLines.Add(token.ToString(Formatting.None));
    }
    
    return string.Join(Environment.NewLine, ndjsonLines);
}
关键注意事项
  • 必须关闭缩进:NDJSON要求每个JSON对象占一行,不能有换行或缩进,所以序列化时一定要设置Formatting.None(Json.NET)或WriteIndented = false(System.Text.Json)。
  • 去掉外层数组:NDJSON没有外层的[]和对象间的逗号,每个对象单独占一行。
  • BigQuery兼容性:生成的NDJSON可以直接用于BigQuery的批量插入,不管是通过API上传文件,还是用BigQuery .NET客户端库的流式插入。

内容的提问来源于stack exchange,提问作者Mayank

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 03:13:32