相关疑难解决方法(0)

如何以编程方式美化JSON?

你知道JavaScript的"JSON Beautifier"吗?

{"name":"Steve","surname":"Jobs","company":"Apple"}
Run Code Online (Sandbox Code Playgroud)

{
  "name" : "Steve",
  "surname" : "Jobs",
  "company" : "Apple"
}
Run Code Online (Sandbox Code Playgroud)

some_magic(jsonObj); // return beautified JSON
Run Code Online (Sandbox Code Playgroud)

javascript json code-formatting pretty-print

326
推荐指数
1
解决办法
16万
查看次数

C#中的JSON格式化程序?

寻找一个将stringJson作为输入的函数,并使用换行符和缩进对其进行格式化.验证将是一个奖励,但不是必需的,我不需要将其解析为对象或任何东西.

有人知道这样的图书馆吗?


样本输入:

{"status":"OK", "results":[ {"types":[ "locality", "political"], "formatted_address":"New York, NY, USA", "address_components":[ {"long_name":"New York", "short_name":"New York", "types":[ "locality", "political"]}, {"long_name":"New York", "short_name":"New York", "types":[ "administrative_area_level_2", "political"]}, {"long_name":"New York", "short_name":"NY", "types":[ "administrative_area_level_1", "political"]}, {"long_name":"United States", "short_name":"US", "types":[ "country", "political"]}], "geometry":{"location":{"lat":40.7143528, "lng":-74.0059731}, "location_type":"APPROXIMATE", "viewport":{"southwest":{"lat":40.5788964, "lng":-74.2620919}, "northeast":{"lat":40.8495342, "lng":-73.7498543}}, "bounds":{"southwest":{"lat":40.4773990, "lng":-74.2590900}, "northeast":{"lat":40.9175770, "lng":-73.7002720}}}}]} 
Run Code Online (Sandbox Code Playgroud)

c# formatting json

94
推荐指数
10
解决办法
10万
查看次数

我可以在ASP.Net项目的代码隐藏中使用JSON.Stringify

在ASP.NET项目(MVP模式)的代码隐藏中,我在其中一个演示者中获得了一个字符串,其中包含一些看起来像JSON文件内容的字符串.

然后我用该字符串设置视图的一个属性 - 分配给演示者.

在视图中,字符串显示在TextBox中,但它看起来不太好,因为它不是使用换行符和换行符构造的.我知道有一个名为Stringify的JSON函数可以使这些字符串变得漂亮.

我可以在代码隐藏中调用JSON函数吗?每个例子我在演示者中设置视图的属性?

所以我在演示者中设置它:

this.view.ContentAsJson = GetContentAsJson(uuid);
Run Code Online (Sandbox Code Playgroud)

这是我想做的,如果可能的话:

this.view.ContentAsJson = JSON.Stringify(GetContentAsJson(uuid));
Run Code Online (Sandbox Code Playgroud)

GetContentAsJson 是一个创建并返回JSON字符串的函数.

这是我的看法:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ContentJsonView.ascx.cs" Inherits="WebCenter.PP.PI.WebGui.View.FolderView.ContentJsonView" %>
<%@ Import Namespace="WebCenter.PP.Common.Domain" %>
<div id="DivContentJson" class="clearfix">
    <p>
        <asp:TextBox runat="server" ID="TbContentJson" TextMode="MultiLine" Height="100%" Width="100%" />
    </p>
</div>
Run Code Online (Sandbox Code Playgroud)

这是视图中获取字符串的属性:

public string ContentAsJson
{
   set
   {
       if (!string.IsNullOrEmpty(value))
       {
            TbContentJson.Text = value;
       }
       else
       {
            TbContentJson.Text = "";
       }
   }
}
Run Code Online (Sandbox Code Playgroud)

c# asp.net json

13
推荐指数
2
解决办法
4万
查看次数

有什么方法可以将google protobuf消息转换为带有缩进的json?

我需要使用 C#将Google protobuf IMessage 对象保存到 json 文件中。这是示例代码:

using (var input = File.OpenRead(protodatFile)) 
{
    string jsonString = null;
    message.MergeFrom(input); //read message from protodat file
    JsonFormatter formater = new JsonFormatter(
        new JsonFormatter.Settings(false));
    jsonString = formatter.Format(message);
    System.IO.File.WriteAllText(jsonFile, jsonString);
}
Run Code Online (Sandbox Code Playgroud)

这使用JsonFormatter来自 Google Protobuf 库的

问题:所有 json 内容都存储在一行中。当文件很大(> 50 MB)时,很难在文本编辑器中打开/查看。

在这里制作缩进 jsonFile 的最佳方法是什么?

c# indentation protocol-buffers

8
推荐指数
1
解决办法
4908
查看次数