Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to parse JSON response (yesterday I was) #2217

Open
SteMMo33 opened this issue May 28, 2024 · 11 comments
Open

Unable to parse JSON response (yesterday I was) #2217

SteMMo33 opened this issue May 28, 2024 · 11 comments
Labels
awaiting-info Not enough information provided

Comments

@SteMMo33
Copy link

Hi there,
I'm developing a C# app by vs2022.
I installed RestSharp by NuGet --> version 111.1.0
Some JSON replies that until yesterday was parsed, now they are not. (???)

The request is (contains two requests ..):

			var client = InitRest();
			var request = new RestRequest("GetTornei.php", Method.Get);

			List<Torneo> list = new List<Torneo>();
			try
			{
				var list2 = client.GetJson<TorneiQuery>("GetTornei.php");

				var response = client.ExecuteGet<TorneiQuery>(request);
				Debug.WriteLine(response.Content);
				Debug.WriteLine(response.StatusCode);
				if (response.StatusCode == HttpStatusCode.OK)
				{
					//list = response.Data.list;
				}

				statusCode = response.StatusCode;

The class is:

namespace TorneoSB
{
	public class Torneo
	{
		// I membri devono essere 'proprietà' per consentire il parsing JSON

		public string id { get; set; }		// Da query arriva una stringa
		public string nome { get; set; }
	}

}

The response is the following (copy and pasted from the debugger:

" {\"list\":[{\"id\":\"2020\",\"nome\":\"Prova torneo - Trofeo\"},{\"id\":\"2023\",\"nome\":\"36\\u00b0 Torneo - Trofeo Ermes Fontana\"}],\"ret\":\"ok\"}"

The response is without any exception, I set the client members as

			ThrowOnDeserializationError = true,
			FailOnDeserializationError = true,

and Data is null without errors.

Screenshot 2024-05-28 160354

If I visualize the Content member with the JSON viewer I correctly see:
Screenshot 2024-05-28 160456

Just one note: the contentType is text/html --> is it misunderstood ??

Thanks!

@SteMMo33 SteMMo33 added the bug label May 28, 2024
@SteMMo33
Copy link
Author

I recovered my last version of the project: it had restsharp 110.2.0 and it was Ok.
Hope it help .

@alexeyzimarev
Copy link
Member

What's the code for TorneiQuery?

Also, text/html is apparently wrong as the response is JSON and it should have an appropriate content type, but it should work regardless.

@alexeyzimarev
Copy link
Member

This would work:

var response = client.ExecuteGet<Torneo[]>(request);

@SteMMo33
Copy link
Author

Sorry I missed the class(es):

		public class TorneoQuery
		{
			public string id { get; set; }
			public string nome { get; set; }
		}

		class TorneiQuery
		{
			public string ret { get; set; }
			public List<TorneoQuery> list { get; set; }
		}

I returned to 110.2.0 and I have no problems.

@alexeyzimarev
Copy link
Member

alexeyzimarev commented Jun 3, 2024

When you say this

Some JSON replies that until yesterday was parsed, now they are not. (???)

what does it mean?

@SteMMo33
Copy link
Author

SteMMo33 commented Jun 3, 2024

Sorry .. simply that in that day I forgot that I had a library update to 111.1.0.
Then I restored restsharp version 110.2.0 and since that day the parses were Ok.

@alexeyzimarev
Copy link
Member

That I understand, but I still don't know what you've got with 111.0

@SteMMo33
Copy link
Author

SteMMo33 commented Jun 4, 2024

Version 111.1.0 does not parse the json string that 110.2.0 does.

@alexeyzimarev
Copy link
Member

Sorry, I don't know how to help you. If you can create a reproduction repo, it can be fixed. Otherwise, it's too hard as you have some JSON and some classes on your side, which I can't see. RestSharp has a bunch of tests for JSON calls and all of them pass on the latest version, so I am not sure what is wrong there.

@SteMMo33
Copy link
Author

Today I updated the verion to the lastone at this moment: 111.3.0.
Again the parse is failed but the StatusCode=OK and no any Exceptions.
The Content is:

 {"list":[{"id":"2020","nome":"Prova torneo - Trofeo"},{"id":"2023","nome":"36\u00b0 Torneo - Trofeo Ermes Fontana"}],"ret":"ok","user":"username","pass":"password","digest":null}

The error is that response.Data is null.
The code is:

var response = client.ExecuteGet<TorneiQuery>(request);

and the class(es) is:

	public class TorneoQuery
	{
		public string id { get; set; }
		public string nome { get; set; }
	}

	class TorneiQuery
	{
		public string ret { get; set; }
		public List<TorneoQuery> list { get; set; }
	}

Again, with version 110.2.0 everyting is ok.
Hope this help .. thanks

@alexeyzimarev
Copy link
Member

Please spend 10 minutes of your time and compose a repro available for public in a GitHub repository.

@alexeyzimarev alexeyzimarev added awaiting-info Not enough information provided and removed bug labels Aug 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting-info Not enough information provided
Projects
None yet
Development

No branches or pull requests

2 participants