Hi
I have below code but it is giving error on this line
dynamic videoListData = JsonConvert.DeserializeObject(videoListResponse.Content);
| Name | Value | Type | |
|---|---|---|---|
| ? | $exception | {"Unexpected character encountered while parsing value: <. Path '', line 0, position 0."} | Newtonsoft.Json.JsonReaderException |
var videoIds = new List();
var client = new RestClient("googleapis.com/youtube/analytics/v1/reports");
var request = new RestRequest("query", Method.GET);
request.AddParameter("ids", "channel==UCg3Zpk-m_nNoaVw");
request.AddParameter("metrics", "views");
request.AddParameter("dimensions", "insightTrafficSourceType,insightTrafficSourceDetail");
request.AddParameter("key", "AIG8");
request.AddParameter("type", "video");
request.AddParameter("maxResults", "50");
do
{
var videoListResponse = client.Execute(request);
dynamic videoListData = JsonConvert.DeserializeObject(videoListResponse.Content);
foreach (var item in videoListData.items)
{
videoIds.Add(item.id.videoId.ToString());
}
// Check if there are more pages of results
if (videoListData.nextPageToken != null)
{
request.AddParameter("pageToken", videoListData.nextPageToken);
}
else
{
break; // No more pages, exit the loop
}
} while (true);
Ramco RamcoPosted Apr 16, 2024, 2:09 PM
Hi All
It is giving below messsage
Thanks
Jayraj ChhayaPosted Apr 16, 2024, 6:55 AM
The error message "Unexpected character encountered while parsing value" suggests that there is an issue with the JSON data being returned by the API. It seems that the response content is not valid JSON.
To troubleshoot this issue, you can try the following steps:
Check the API documentation: Make sure you are using the correct endpoint and parameters for the YouTube Analytics API. Verify that the response format is indeed JSON.
Inspect the response content: Print the
videoListResponse.Contentto the console or log it to see the actual response content. This will help you identify any unexpected characters or formatting issues.Validate the JSON: Use an online JSON validator or a JSON parsing library to validate the response content. This will help you determine if the JSON is valid or if there are any syntax errors.
Handle potential errors: Wrap the deserialization code in a try-catch block to catch any exceptions that may occur during the deserialization process. This will allow you to handle the error gracefully and provide a more meaningful error message to the user.
Naimish MakwanaPosted Apr 16, 2024, 5:01 AM
The error message you’re seeing,
Unexpected character encountered while parsing value: <. Path '', line 0, position 0, is typically thrown byJsonConvert.DeserializeObjectwhen it’s trying to parse a string that isn’t valid JSON.In your case, it seems like the response from the API call (
videoListResponse.Content) is not in valid JSON format. This could be due to a few reasons:The API endpoint might not be returning a JSON response. The URL you’re using (
"googleapis.com/youtube/analytics/v1/reports") seems to be incorrect. The correct base URL for YouTube Data API v3 should be"https://www.googleapis.com/youtube/v3/". Please check the API documentation and ensure you’re using the correct endpoint.There might be an issue with the API request, such as an incorrect parameter or header, causing the API to return an error page (often in HTML, hence the
<character) instead of the expected JSON response.The API might be returning a JSON response, but the structure of the response might be different from what you’re expecting (i.e., it doesn’t contain
itemsornextPageTokenproperties).To debug this, you can try printing out
videoListResponse.Contentbefore attempting to deserialize it. This will allow you to see exactly what’s being returned by the API call.This should give you a better idea of what’s going wrong. If the output is not valid JSON, you’ll need to adjust your API request or use the correct API endpoint. If the JSON structure is different from what you’re expecting, you’ll need to adjust your code to correctly navigate the JSON response.
Thanks