使用 JSON.net 我可以按照此 link 中的回答进行操作
string content = File.ReadAllText(path);
var token = JToken.Parse(content);
if (token is JArray)
{
IEnumerable<Phone> phones = token.ToObject<List<Phone>>();
}
else if (token is JObject)
{
Phone phone = token.ToObject<Phone>();
}
但是我可以在 ServiceStack.Text 库中做类似的事情吗?
最佳答案
你可以这样做:
string content = File.ReadAllText(path);
if (JsonUtils.IsJsArray(content))
{
IEnumerable<Phone> phones = JsonSerializer.DeserializeFromString<List<Phone>>(json);
}
else if (JsonUtils.IsJsObject(content))
{
Phone phone = JsonSerializer.DeserializeFromString<Phone>(json);
}
关于c# - 使用 ServiceStack.Text : determine JSON is Array, 对象或字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39933351/