This question already has answers here:
How do I get formatted JSON in .NET using C#?
(14个回答)
3年前关闭。
基本上是对this问题的重复,但有一个显着的区别-我必须使用
一个简单的
产生单行json,例如(当保存在文件中时)
有什么选择呢?
我可以想到后处理...还有更简单的选择吗?例如。类似于格式化xml produced by DataContractSerializer吗?
有没有一种方法可以使
注意线条
对于某些类型的xml序列化器,应使用InvariantCulture来避免在具有不同“区域”设置的计算机上反序列化期间发生异常。例如,double或DateTime的无效格式有时会导致它们。
反序列化
谢谢!
(14个回答)
3年前关闭。
基本上是对this问题的重复,但有一个显着的区别-我必须使用
DataContractJsonSerializer
。一个简单的
using (var stream = new MemoryStream())
{
var serializer = new DataContractJsonSerializer(typeof(Person));
serializer.WriteObject(stream, obj);
...
return stream.ToArray();
}
产生单行json,例如(当保存在文件中时)
...{"blah":"v", "blah2":"v2"}...
有什么选择呢?
...
{
"blah":"v",
"blah2":"v2"
}
...
我可以想到后处理...还有更简单的选择吗?例如。类似于格式化xml produced by DataContractSerializer吗?
using (var stream = new MemoryStream())
{
var serializer = new DataContractJsonSerializer(typeof(T));
// "beautify"
using (var writer = new SomeKindOfWriter(stream))
serializer.WriteObject(writer, obj);
...
return stream.ToArray();
}
有没有一种方法可以使
SomeKindOfWriter
在需要时美化json? 最佳答案
https://stackoverflow.com/a/38538454/6627992
您可以使用以下标准方法来格式化Json
JsonReaderWriterFactory.CreateJsonWriter(流流,编码编码,bool ownsStream,bool缩进,字符串indentChars)
仅设置“indent == true”
尝试这样的事情
public readonly DataContractJsonSerializerSettings Settings =
new DataContractJsonSerializerSettings
{ UseSimpleDictionaryFormat = true };
public void Keep<TValue>(TValue item, string path)
{
try
{
using (var stream = File.Open(path, FileMode.Create))
{
//var currentCulture = Thread.CurrentThread.CurrentCulture;
//Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
try
{
using (var writer = JsonReaderWriterFactory.CreateJsonWriter(
stream, Encoding.UTF8, true, true, " "))
{
var serializer = new DataContractJsonSerializer(type, Settings);
serializer.WriteObject(writer, item);
writer.Flush();
}
}
catch (Exception exception)
{
Debug.WriteLine(exception.ToString());
}
finally
{
//Thread.CurrentThread.CurrentCulture = currentCulture;
}
}
}
catch (Exception exception)
{
Debug.WriteLine(exception.ToString());
}
}
注意线条
var currentCulture = Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
....
Thread.CurrentThread.CurrentCulture = currentCulture;
对于某些类型的xml序列化器,应使用InvariantCulture来避免在具有不同“区域”设置的计算机上反序列化期间发生异常。例如,double或DateTime的无效格式有时会导致它们。
反序列化
public TValue Revive<TValue>(string path, params object[] constructorArgs)
{
try
{
using (var stream = File.OpenRead(path))
{
//var currentCulture = Thread.CurrentThread.CurrentCulture;
//Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
try
{
var serializer = new DataContractJsonSerializer(type, Settings);
var item = (TValue) serializer.ReadObject(stream);
if (Equals(item, null)) throw new Exception();
return item;
}
catch (Exception exception)
{
Debug.WriteLine(exception.ToString());
return (TValue) Activator.CreateInstance(type, constructorArgs);
}
finally
{
//Thread.CurrentThread.CurrentCulture = currentCulture;
}
}
}
catch
{
return (TValue) Activator.CreateInstance(typeof (TValue), constructorArgs);
}
}
谢谢!
关于c# - DataContractJsonSerializer可读的json,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33371606/
10-09 05:17