Newtonsoft.Json 序列化后的数据格式,并不适合阅读、修改。
下面的代码用于美化数据(格式化)。
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
using Newtonsoft.Json;
///
/// 格式化(美观)json文件的类
///
public static class JsonFormater
{
//private static int MIN_LENGTH = 4;
public static string Execute(string str)
{
//格式化json字符串
JsonSerializer serializer = new JsonSerializer();
TextReader tr = new StringReader(str);
JsonTextReader jtr = new JsonTextReader(tr);
object obj = serializer.Deserialize(jtr);
if (obj != null)
{
StringWriter textWriter = new StringWriter();
JsonTextWriter jsonWriter = new JsonTextWriter(textWriter)
{
Formatting = Formatting.Indented,
Indentation = 4,
IndentChar = ' '
};
serializer.Serialize(jsonWriter, obj);
return textWriter.ToString();
}
else
{
return str;
}
}
///
/// 将Unix时间戳转换为DateTime类型时间
///
/// double 型数字
///
private static System.DateTime Double2DateTime(double d)
{
System.DateTime time = System.DateTime.MinValue;
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
time = startTime.AddSeconds(d);
return time;
}
///
/// 将c# DateTime时间格式转换为Unix时间戳格式
///
/// 时间
///
private static double DateTime2Double(System.DateTime time)
{
double intResult = 0.0;
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
intResult = (time - startTime).TotalSeconds;
return intResult;
}
}
POWER BY 315SOFT.COM
TRUFFER.CN
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
using Newtonsoft.Json;
///
/// 格式化(美观)json文件的类
///
public static class JsonFormater
{
//private static int MIN_LENGTH = 4;
public static string Execute(string str)
{
//格式化json字符串
JsonSerializer serializer = new JsonSerializer();
TextReader tr = new StringReader(str);
JsonTextReader jtr = new JsonTextReader(tr);
object obj = serializer.Deserialize(jtr);
if (obj != null)
{
StringWriter textWriter = new StringWriter();
JsonTextWriter jsonWriter = new JsonTextWriter(textWriter)
{
Formatting = Formatting.Indented,
Indentation = 4,
IndentChar = ' '
};
serializer.Serialize(jsonWriter, obj);
return textWriter.ToString();
}
else
{
return str;
}
}
///
/// 将Unix时间戳转换为DateTime类型时间
///
/// double 型数字
/// DateTime
private static System.DateTime Double2DateTime(double d)
{
System.DateTime time = System.DateTime.MinValue;
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
time = startTime.AddSeconds(d);
return time;
}
///
/// 将c# DateTime时间格式转换为Unix时间戳格式
///
/// 时间
/// double
private static double DateTime2Double(System.DateTime time)
{
double intResult = 0.0;
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
intResult = (time - startTime).TotalSeconds;
return intResult;
}
}