我如何格式化的JSON在C#.NET [英] How do i make formatted json in C#.NET

查看:198
本文介绍了我如何格式化的JSON在C#.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的.NET JSON解析器,我想连载我的配置文件,因此它是可读的,而不是

  {嗒嗒:V,blah2:V2}
 

东西更好

  {
    胡说:V,
    blah2:V2
}
 

我的code是像

 使用System.Web.Script.Serialization;

VAR SER =新JavaScriptSerializer();
configSz = ser.Serialize(配置);
使用(VAR F =(的TextWriter)File.CreateText(configFn))
{
    f.WriteLine(configSz);
    f.Close();
}
 

解决方案

您将有一个很难与JavaScriptSerializer实现这一点。

尝试 JSON.Net

从JSON.Net例子稍作修改

 使用系统;
使用Newtonsoft.Json;

命名空间的Json prettyPrint
{
    内部类节目
    {
        私有静态无效的主要(字串[] args)
        {
            产品产品=新产品
                {
                    NAME =苹果,
                    过期=新的日期时间(2008,12,28),
                    价格= 3.99M,
                    尺寸=新的[] {小,中等,大}
                };

            JSON字符串= JsonConvert.SerializeObject(产品,Formatting.Indented);
            Console.WriteLine(JSON);

            产品deserializedProduct = JsonConvert.DeserializeObject<产品>(JSON);
        }
    }

    内部类产品
    {
        公众的String [] {尺寸得到;组; }
        公共十进制价格{获得;组; }
        公开日期时间到期{获得;组; }
        公共字符串名称{;组; }
    }
}
 

结果

  {
  大小:
    小,
    中,
    大
  ]
  价格:3.99,
  到期:\ /日期(1230447600000-0700)\ /,
  名:苹果
}
 

文件: 序列化一个对象

I am using .NET json parser and i would like to serialize my config file so it is readable instead of

{"blah":"v", "blah2":"v2"}

to something nicer like

{
    "blah":"v", 
    "blah2":"v2"
}

my code is something like

using System.Web.Script.Serialization; 

var ser = new JavaScriptSerializer();
configSz = ser.Serialize(config);
using (var f = (TextWriter)File.CreateText(configFn))
{
    f.WriteLine(configSz);
    f.Close();
}

解决方案

You are going to have a hard time accomplishing this with JavaScriptSerializer.

Try JSON.Net.

With minor modifications from JSON.Net example

using System;
using Newtonsoft.Json;

namespace JsonPrettyPrint
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            Product product = new Product
                {
                    Name = "Apple",
                    Expiry = new DateTime(2008, 12, 28),
                    Price = 3.99M,
                    Sizes = new[] { "Small", "Medium", "Large" }
                };

            string json = JsonConvert.SerializeObject(product, Formatting.Indented);
            Console.WriteLine(json);

            Product deserializedProduct = JsonConvert.DeserializeObject<Product>(json);
        }
    }

    internal class Product
    {
        public String[] Sizes { get; set; }
        public decimal Price { get; set; }
        public DateTime Expiry { get; set; }
        public string Name { get; set; }
    }
}

Results

{
  "Sizes": [
    "Small",
    "Medium",
    "Large"
  ],
  "Price": 3.99,
  "Expiry": "\/Date(1230447600000-0700)\/",
  "Name": "Apple"
}

Documentation: Serialize an Object

这篇关于我如何格式化的JSON在C#.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆