【问题标题】:Read a list from an API [closed]从 API 中读取列表 [关闭]
【发布时间】:2021-03-02 14:06:18
【问题描述】:

我正在尝试从 API 读取列表。 这是API返回给我的响应。

[["a",1,2,3,4,5,6,7,8,9,10,11,12], 
 ["b",2,3,4,5,6,7,8,9,10,11,12,13]]

这是我的电话

            HttpResponseMessage response = await _client.GetAsync("url");
            var strings = await response.Content.ReadAsStringAsync();
            var stringResult = strings.Substring(1, allSymbolsString.Length-2).Split(',');
            List<string> stringList = new List<string>();
            List<string> cleanList = stringResult.ToList();
            foreach (var s in cleanList)
            {
                stringList.Add(Regex.Replace(s, @"...", ""));
            }

            stringList.RemoveAll(s => string.IsNullOrWhiteSpace(s));
            List<model> m = new List<model> { };
            foreach (var s in stringList)
            {
                m.Add(new model
                {
                    .
                    .
                    .
                });
            }

            return m;

这对我有用,但我想知道,有没有更短的方法?我尝试以流的形式读取,但它给了我一个错误。所以我是这样写的。 我希望我的问题很清楚。 谢谢。

【问题讨论】:

    标签: c# asp.net-core asp.net-web-api2


    【解决方案1】:

    API 返回的是 JSON。 你不能反序列化成一个字符串[][]?

    如果您使用的是 JSON.NET:

    JsonConvert.DeserializeObject<string[][]>(strings);
    

    【讨论】:

    • 公平警告:System.Text.Json 中的等效项将不起作用,因为源 JSON 包含数字。您必须使用 object[][] 作为类型并转换为字符串。
    猜你喜欢
    • 2016-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多