留言 = "Unexpected character encountered while parsing value: [. Path '', line 1, position 1."

Message = "Unexpected character encountered while parsing value: [. Path '', line 1, position 1."

我想在我的 ASP.NET 核心 MVC 视图中显示部门列表。

请注意,我有 3 层(数据访问 + API(从数据库获取数据)+ MVC (UI))。

这是我的 Json 数据,我通过调用 API:

从数据库中获得
[
  {
    "id": 3,
    "name": "Sales"
  },
  {
    "id": 4,
    "name": "PMO"
  },
  {
    "id": 5,
    "name": "Research And Development"
  },
  {
    "id": 6,
    "name": "Product Management"
  },
  {
    "id": 7,
    "name": "HR"
  },
  {
    "id": 8,
    "name": "Ava"
  },
  {
    "id": 9,
    "name": "IT"
  }
]

这是我的 C# 代码,使用 HttpClient 从 API:

获取数据
public async Task<T> GetRequest<T>(string uri)
{
        try
        {
            var client = _httpClientFactory.CreateClient();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            using (HttpResponseMessage response = await client.GetAsync(uri))
            {
                if (response.StatusCode.ToString() == "OK")
                {
                    _logger.LogInformation("Get Request Successed");
                    //response.EnsureSuccessStatusCode();
                   responseBody = await response.Content.ReadAsStringAsync();
                }

                return JsonConvert.DeserializeObject<T>(responseBody);
            }
        }
        catch (Exception ex)
        {
            _logger.LogError("Failed");
            return JsonConvert.DeserializeObject<T>(responseBody);
        }
}

当我试图解析来自 json 的数据时,它 returns 出错了。

Unexpected character encountered while parsing value: [. Path '', line 1, position 1.

您可以将 string 解析为强类型 Model,然后将 de-serialize 解析为它,或者您可以使用 dynamic 并访问如下所示的属性:

你可以找到一个有效的例子here

using System;
using Newtonsoft.Json;
using System.Collections.Generic;
                    
public class Program
{
    public static void Main()
    {
        var myJsonString=@"[{'id':3,'name':'Sales'},{'id':4,'name':'PMO'},{'id':5,'name':'Research And Development'},{'id':6,'name':'Product Management'},{'id':7,'name':'HR'},{'id':8,'name':'Ava'},{'id':9,'name':'IT'}]";
        var result =JsonConvert.DeserializeObject<List<Root>>(myJsonString);
        Console.WriteLine("Example using Model: \n");
        foreach(var item in result)
        {
            Console.WriteLine(item.id);
            Console.WriteLine(item.name);           
        }
        
        Console.WriteLine("\n");
        Console.WriteLine("Example using Dynamic: \n");
        
        //Example using dynamic
        var resultDynamic=JsonConvert.DeserializeObject<dynamic>(myJsonString);
        foreach(var item in resultDynamic)
        {
            Console.WriteLine(item["id"]);
            Console.WriteLine(item["name"]);        
        }
    }
}

public class Root
{
    public int id { get; set; }
    public string name { get; set; }
}

输出:

Example using Model: 

3
Sales
4
PMO
5
Research And Development
6
Product Management
7
HR
8
Ava
9
IT


Example using Dynamic: 

3
Sales
4
PMO
5
Research And Development
6
Product Management
7
HR
8
Ava
9
IT