【问题标题】:using ServiceStack.Text: determine JSON is Array, Object or String?使用 ServiceStack.Text:确定 JSON 是 Array、Object 还是 String?
【发布时间】:2016-10-09 01:52:17
【问题描述】:

使用 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 库中进行类似的操作?

【问题讨论】:

    标签: c# json servicestack json-deserialization servicestack-text


    【解决方案1】:

    你可以这样做:

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

    【讨论】:

      猜你喜欢
      • 2017-12-21
      • 1970-01-01
      • 1970-01-01
      • 2021-12-31
      • 2011-09-01
      • 2023-02-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多