Matt Matt - 1 month ago 26
JSON Question

How to convert a txt stream web request containing json to a jObject?

Trying to use a query Google has available, but they return an attached txt file containing the JSON results. I'm a newbie programmer, so I can't figure out why any of the shots I took aren't working.

public async Task<YouTubeSearchResult> SearchYouTubeAsync(string query)
{
var result = new YouTubeSearchResult();
string errorMessage = "";
try
{
string encodedName = WebUtility.UrlEncode(query);
Uri url = new Uri($"http://suggestqueries.google.com/complete/search?client=firefox&ds=yt&q={encodedName}");
HttpClient client = new HttpClient();
Stream streamResult = await client.GetStreamAsync(url);
StreamReader reader = new StreamReader(streamResult);

errorMessage = JsonConvert.SerializeObject(reader.ReadToEnd());
JObject jsonResults = JObject.Parse(JsonConvert.SerializeObject(reader.ReadToEnd()));

result.Success = true;
result.Message = "Success getting search results";
result.SearchResults = jsonResults;
}
catch (Exception ex)
{
result.Success = false;
result.Message = $"Server error getting search results: {errorMessage} | {ex}";
result.SearchResults = null;
}

return result;

}
}


This is the response along with the error code I get.

{
"success": false,
"message": "Server error getting search results: \"[\"search\",[\"search\",\"search and destroy\",\"searching for my baby bobby moore\",\"search engine optimization\",\"search and discard\",\"search for the worst\",\"search youtube\",\"searching\",\"search history\",\"search party sam bruno\"]]\" | Newtonsoft.Json.JsonReaderException: Error reading JObject from JsonReader. Current JsonReader item is not an object: String. Path '', line 1, position 2.\r\n at Newtonsoft.Json.Linq.JObject.Load(JsonReader reader, JsonLoadSettings settings)\r\n at Newtonsoft.Json.Linq.JObject.Parse(String json, JsonLoadSettings settings)\r\n at OdsCode.Services.YouTubeSearchService.<SearchYouTubeAsync>d__3.MoveNext()",
"searchResults": null
}


Adding the error and current results separately to clarify.


| Newtonsoft.Json.JsonReaderException: Error reading JObject from
JsonReader. Current JsonReader item is not an object: String. Path '',
line 1, position 2.\r\n at
Newtonsoft.Json.Linq.JObject.Load(JsonReader reader, JsonLoadSettings
settings)\r\n at Newtonsoft.Json.Linq.JObject.Parse(String json,
JsonLoadSettings settings)\r\n at
OdsCode.Services.YouTubeSearchService.d__3.MoveNext()",

"Server error getting search results:
\"[\"search\",[\"search\",\"search and destroy\",\"searching for my
baby bobby moore\",\"search and discard\",\"search for the
worst\",\"search youtube\",\"search engine
optimization\",\"searching\",\"search history\",\"search party sam
bruno\"]]\" |


Below is the response I get from google in postman

http://suggestqueries.google.com/complete/search?client=firefox&ds=yt&q=search

Cache-Control →no-cache, must-revalidate
Content-Disposition →attachment; filename="f.txt"
Content-Encoding →gzip
Content-Length →136
Content-Type →text/javascript; charset=UTF-8
Date →Wed, 19 Oct 2016 20:10:17 GMT
Expires →-1
Pragma →no-cache
Server →gws
X-Frame-O

ptions →SAMEORIGIN
X-XSS-Protection →1; mode=block

[
"search",
[
"search",
"search and destroy",
"searching for my baby bobby moore",
"search engine optimization",
"search and discard",
"search for the worst",
"search youtube",
"searching",
"search history",
"search party sam bruno"
]
]


Help me I been at it for days now... No food until I figure this out!!!!!

Answer

Problem one - you use reader.ReadToEnd() twice. First when you attempt to read errorMessage, Then on the next line you use it again. By the second time you have already read everything. Delete the line:

errorMessage = JsonConvert.SerializeObject(reader.ReadToEnd());

Problem two - looks like the data you are receiving is an array so in order to read it you will need to use

JArray jsonResults = JArray.Parse(reader.ReadToEnd());