简体   繁体   English

如何在类似于 JavaScript 的 C# .Net Core 中轻松创建可读的 JSON 对象

[英]How can I easily create readable JSON objects in C# .Net Core similar to JavaScript

In JavaScript I can easily create an object and assign variables with some very basic notation such as:在 JavaScript 中,我可以轻松地创建一个对象并使用一些非常基本的符号分配变量,例如:

const Object = JSON.parse('
  {
    "something": outsideVariable,
    "someArray": [
      "hey",
      "there"
    ]
  }
');

Is there a simple way to do this with C# that is clean and easy to assign variables too?是否有一种简单的方法可以使用 C# 来做到这一点,既干净又易于分配变量? I experimented a bit with the JsonObject but the code for that looks needlessly messy, for example:我对 JsonObject 进行了一些试验,但它的代码看起来不必要的混乱,例如:

JsonObject jsonPayload = new JsonObject
    {
        ["documentId"] = documentID,
        ["testMode"] = true,
        ["signers"] = new JsonArray
        {
            new JsonObject
            {
                ["label"] = "John Smith",
                ["contactMethod"] = new JsonArray
                {
                    new JsonObject
                    {
                        ["type"] = "link"
                    }
                }
            }
        }
    };

I also tried using the literal symbol (@) with a string, but it ends up injecting carriage returns and linefeeds, and inserting variables winds of being a great deal of concatination.我还尝试将文字符号 (@) 与字符串一起使用,但它最终会注入回车符和换行符,并且插入变量会导致大量的串联。

IMHO , the most close you want is this恕我直言,你想要的最接近的是这个

var jsonPayload  = new
    {
        documentId = 1,
        testMode = true,
        signers = new object[]
        {
          new {
             label="John Smith",
            contactMethod= new object[]
            {
              new {
                   type="link"
              }
            }
          }
        }
    };

and code和代码

    
var json= System.Text.Json.JsonSerializer
           .Serialize(jsonPayload,new JsonSerializerOptions { WriteIndented = true }); 

//or if you need
var jsonParsed= JsonDocument.Parse(json);

result结果

{
  "documentId": 1,
  "testMode": true,
  "signers": [
    {
      "label": "John Smith",
      "contactMethod": [
        {
          "type": "link"
        }
      ]
    }
  ]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Excel中创建可读的表? C# - How can I create a table readable in excel? C# 如何在C#(。net核心)中将对象数组转换为JSON对象 - How Do I convert my array of objects into JSON object in C# (.net core) 我可以在 Google Sheets .NET API 中使用直接 JSON 请求来创建图表而不是 C# .NET 对象吗? - Can I use direct JSON request in Google Sheets .NET API to create charts instead of C# .NET objects? 如何使用 Object c# .NET Core 在 blob 存储 azure 上创建 csv 文件? - How can I create a csv file on blob storage azure with Object c# .NET Core? 使用 Json.NET,如何合并两个需要序列化的 C# 对象? - With Json.NET, how can I merge two C# objects that need to be serialized together? 如何重构我的 C# 代码以使其易于测试? - How can I refactor my C# code to be easily testable? C#JSON.NET如何创建通用电报? - C# JSON.NET How can I create generic telegram? 我可以在C#数组列表中存储类似的对象 - can I store similar objects in a C# array list 如何在C#中创建JSON文本对象 - How to create objects of JSON text in c# 使用C#中的JSON(或其他易于阅读的文本格式)中的数据填充现有SQLite数据库 - Fill existing SQLite database with data from JSON (or other easily-readable text format) in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM