你知道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) 寻找一个将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) 在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#将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 的最佳方法是什么?