简体   繁体   中英

using ServiceStack.Text: determine JSON is Array, Object or String?

using JSON.net I could do this as answered in this 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>();
}

but is there a way i could do it similarly in ServiceStack.Text library?

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM