[英]using ServiceStack.Text: determine JSON is Array, Object or String?
using JSON.net I could do this as answered in this link 使用JSON.net我可以在此链接中回答这个问题
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>();
}
but is there a way i could do it similarly in ServiceStack.Text library? 但有没有办法在ServiceStack.Text库中类似地做到这一点?
You could do it like this: 你可以这样做:
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.