온라인 POCO 생성기를 사용하더라도 현재 JSON 배열 (Auth0 관리 API 끝점)을 역 직렬화 할 수 없습니다.

어치

이 문제는 Auth0 관리 API 끝점으로 작업하고 있습니다.

https://auth0.com/docs/api/management/v2?_ga=2.197148647.957265821.1601726170-1678439180.1588036522#!/Users/get_users

다음은 내 휴식 코드입니다.

    var client = new RestClient(tempapiendpoint);
    var request = new RestRequest(Method.GET);
    request.AddHeader(header, bearerstring);
    request.AddParameter(specificfieldname,specificfields);
    request.AddParameter(includefieldname, includetrueorfalse);
    IRestResponse response = await client.ExecuteAsync(request);
    var myDeserializedClass = JsonConvert.DeserializeObject<Root>(response.Content);

다음과 같은 응답이 있습니다.

[
  {
    "email": "somevalue",
    "name": "somevalue",
    "nickname": "somevalue",
    "user_id": "somevalue"
  },
  {
    "email": "somevalue",
    "name": "somevalue",
    "nickname": "somevalue",
    "user_id": "somevalue"
  },
  {
    "email": "somevalue",
    "name": "somevalue",
    "nickname": "somevalue",
    "user_id": "somevalue"
  }
]

이 시점에서 https://json2csharp.com/ 과 같은 온라인 클래스 생성기를 사용합니다.

// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse); 
    public class MyArray    {
        [JsonProperty("email")]
        public string Email; 

        [JsonProperty("name")]
        public string Name; 

        [JsonProperty("nickname")]
        public string Nickname; 

        [JsonProperty("user_id")]
        public string UserId; 
    }

    public class Root    {
        [JsonProperty("MyArray")]
        public List<MyArray> MyArray; 
    }

매번 같은 오류가 발생합니다.

Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'RandomStuffGeneratorPrivate.POCO.Root' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.

To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.

Path '', line 1, position 1.

내가 시도한 몇 가지 더.

  • 클래스 생성을 위해 https://app.quicktype.io/ 다른 소스를 시도했습니다 . 똑같은 오류가 발생합니다.
  • json의 유효성을 확인했습니다. 올바른 형식입니다.
  • 손으로 매핑하더라도 이것은 직선적 인 json입니다.
  • 또한 사용자 클래스의 이름과 컬렉션 이름이 모두 동일하다는 것을 알았습니다. 그래서 사용자 클래스의 이름을 변경했습니다. (디버깅하는 동안 오류가 발생하지 않았지만 어쨌든 변경했습니다). 오류 변경 없음
  • 클래스를 생성 할 때 온라인 API 문서가 실수를하는 경우를 대비하여 IRestResponse response.content에서 디버깅하는 동안 라이브 응답에서 직접 json 문자열을 가져옵니다.
  • 다른 스택 질문을 살펴본 결과 목록이없는 것과 관련된 실수가 있음을 발견했습니다. 여기서는 반환 배열을 목록에 매핑했습니다 (온라인 생성기는 이러한 실수를하지 않습니다).
도마

잘못된 클래스로 역 직렬화하고있는 것 같습니다. JsonConvert.DeserializeObject <MyArray []> (myJsonResponse)

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관