嘗試使用Google提供的查詢,但它們返回包含JSON結果的附加txt文件。我是一個新手程序員,所以我無法弄清楚爲什麼我拍攝的任何鏡頭都不能正常工作。如何將包含json的txt流web請求轉換爲jObject?
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;
}
}
這是響應以及我得到的錯誤代碼。
{
"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
}
單獨添加錯誤和當前結果以闡明。
| Newtonsoft.Json.JsonReaderException:從 JsonReader讀取JObject時出錯。當前的JsonReader項目不是一個對象:String。路徑「, 第1行,位置2. \ r \ n在 Newtonsoft.Json.Linq.JObject.Load(JsonReader reader,JsonLoadSettings settings)\ r \ n at Newtonsoft.Json.Linq.JObject.Parse(String JSON, JsonLoadSettings設置)\ r \ n在 OdsCode.Services.YouTubeSearchService.d__3.MoveNext() 「
」服務器錯誤得到的搜索結果: \「[\ 」搜索\「[\」 搜索\「搜索並銷燬\」,\「搜索我的 寶貝鮑比摩爾\」,\「搜索丟棄\」,\「搜索最差的\」 \「,\」搜索youtube \「,\」搜索引擎 優化\「,\」搜索\「,\」搜索歷史\「,\」搜索方sam bruno \「]] \」|
下面是郵遞員,我從谷歌得到的迴應
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"
]
]
幫助我,我一直盯着它現在天...沒有食物,直到我想出解決辦法!!!!!
您的接收端。不要使用'JsonConvert.SerializeObject'。 'JObject.Parse(reader.ReadToEnd())'是全部。 (object2json - >序列化和json2object - >反序列化) –
當我將它關閉時,我得到以下內容→Newtonsoft.Json.JsonReaderException:從JsonReader讀取JObject時出錯。 Path'',第0行,位置0. \ New \設置)\ r \ n在OdsCode.Services.YouTubeSearchService。 d__3.MoveNext() –
Matt