修剪JSON字段,不适用于嵌套的JSON
本文关键字:JSON 嵌套 适用于 字段 修剪 不适用 | 更新日期: 2023-09-27 17:59:47
我想修剪JSON中的所有字符串字段。我使用它自己的JSonConverter(代码如下,Newtonsoft),它是在global.asax中的MVC应用程序启动中添加的。如果没有嵌套的JSON,一切都会很好。如果存在,则不处理嵌套的JSON。为什么?我应该更改什么来修复它?
例如
{"Name":" Jacek ","Age" = " 10 "} working
{"Name":" Jacek ","Age"=" 10 "."Address":{"street":" Long "}} NOT working for street, but works for name and age.
我的字符串转换器字符串
public class MyStringConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(string);
}
public override bool CanRead { get { return true; } }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var text = (string)reader.Value;
return TrimInputField(text);
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
writer.WriteValue(value);
}
public string TrimInputField(string input)
{
if (false == string.IsNullOrEmpty(input))
{
input = input.Trim();
var regex = new Regex(@"'s+");
input = regex.Replace(input, " ");
}
return input;
}
}
",",而不是"。"{"Name":"Jacek","Age"="10","Address":{"street":"Long"}}