当值可以是对象或空数组时,反序列化JSON

我正在使用VK API。有时服务器可以返回空数组而不是对象,例如:

personal: [] //when it is empty

要么

personal: {

religion: 'Нет',

smoking: 1,

alcohol: 4

} //when not empty.

我用JsonConvert.DeserializeObject反序列化大多数json,而这部分json用

MainObject = ((MainObject["response"].GetObject())["user"].GetObject())["personal"].GetObject();

try

{

Convert.ToByte(MainObject["political"].GetNumber();

}

catch {}

但是,当它处理大量肽时,它会使应用程序运行缓慢。刚才我意识到,这里还有其他一些字段,如果为空,它们可能会返回数组。我只是不知道如何快速而清晰地做到这一点。有什么建议?

我的反序列化类(当字段为空时不起作用):

     public class User

{

//some other fields...

public Personal personal { get; set; }

//some other fields...

}

public class Personal

{

public byte political { get; set; }

public string[] langs { get; set; }

public string religion { get; set; }

public string inspired_by { get; set; }

public byte people_main { get; set; }

public byte life_main { get; set; }

public byte smoking { get; set; }

public byte alcohol { get; set; }

}

另一个想法(不为空时不起作用):

public List<Personal> personal { get; set; }

回答:

您可以JsonConverter像下面这样,查找指定类型的对象或空数组。如果是对象,它将反序列化该对象。如果为空数组,则返回null:

public class JsonSingleOrEmptyArrayConverter<T> : JsonConverter where T : class

{

public override bool CanConvert(Type objectType)

{

return typeof(T).IsAssignableFrom(objectType);

}

public override bool CanWrite { get { return false; } }

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)

{

var contract = serializer.ContractResolver.ResolveContract(objectType);

if (!(contract is Newtonsoft.Json.Serialization.JsonObjectContract || contract is Newtonsoft.Json.Serialization.JsonDictionaryContract))

{

throw new JsonSerializationException(string.Format("Unsupported objectType {0} at {1}.", objectType, reader.Path));

}

switch (reader.SkipComments().TokenType)

{

case JsonToken.StartArray:

{

int count = 0;

while (reader.Read())

{

switch (reader.TokenType)

{

case JsonToken.Comment:

break;

case JsonToken.EndArray:

return existingValue;

default:

{

count++;

if (count > 1)

throw new JsonSerializationException(string.Format("Too many objects at path {0}.", reader.Path));

existingValue = existingValue ?? contract.DefaultCreator();

serializer.Populate(reader, existingValue);

}

break;

}

}

// Should not come here.

throw new JsonSerializationException(string.Format("Unclosed array at path {0}.", reader.Path));

}

case JsonToken.Null:

return null;

case JsonToken.StartObject:

existingValue = existingValue ?? contract.DefaultCreator();

serializer.Populate(reader, existingValue);

return existingValue;

default:

throw new InvalidOperationException("Unexpected token type " + reader.TokenType.ToString());

}

}

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)

{

throw new NotImplementedException();

}

}

public static partial class JsonExtensions

{

public static JsonReader SkipComments(this JsonReader reader)

{

while (reader.TokenType == JsonToken.Comment && reader.Read())

;

return reader;

}

}

然后像这样使用它:

public class User

{

//some other fields...

[JsonConverter(typeof(JsonSingleOrEmptyArrayConverter<Personal>))]

public Personal personal { get; set; }

//some other fields...

}

现在,您应该可以将用户反序列化到您的User班级中。

笔记:

  • 可以通过属性或在中应用转换器JsonSerializerSettings.Converters

  • 该转换器并非设计为与简单类型(例如字符串)一起使用,而是为映射到JSON对象的类设计的。那是因为它JsonSerializer.Populate()用来避免读取过程中的无限递归。

工作样本.Net

在这里和这里摆弄。

以上是 当值可以是对象或空数组时,反序列化JSON 的全部内容, 来源链接: utcz.com/qa/411076.html

回到顶部