a bug?webapi can't response object curectly when contain a uncertain type of dictionary

志彬 李 0 Reputation points
2023-03-16T02:59:14.84+00:00

when i try to return object with a uncertain value type of dictionary object,the api return a wrong data.

the below is the code:

webapi:


public PagerList<CronJob>GetList(int curPage=1,int pageSize=20) // 
{ 
  PagerList<Hello> l = cronJobService.GetPagerList(curPage, pageSize); 
  return l; 
}

Hello Class

public class Hello { 

/// <summary> 

/// 

///</summary> 

public int Id { get; set; } 

/// <summary> 

/// name 

///</summary> 

public string Name { get; set; } 

/// <summary> 

/// parameter;{} 

///</summary> 

public Dictionary<string,object> Parameter { get; set; } } 


when i call the api,i can get the list like below through debug

[{  "id": 2,  "name": "hello",  "parameter": {    "time": [      15,     7,      1    ]  }}]

but i get the below data through browser

[{ "id": 2, "name": "hello", "parameter": { "time": [ [], [], [] ] } }]

as we see,the parameter attribute is a wrong data.

how can i get the right data?

Developer technologies | .NET | Other
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. 志彬 李 0 Reputation points
    2023-03-16T03:43:34.27+00:00

    i have found the issue:

    the controller parse the array value into JArray,and i didn't add newtonjson,so the issue fixed after i use flow code:

    services.AddControllers()
            .AddNewtonsoftJson(); //add newtonjson 
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.