簡體   English   中英

使用ServiceStack.Text:確定JSON是Array,Object還是String?

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

使用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>();
}

但有沒有辦法在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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM