如何将包含 json 的 txt 流 Web 请求转换为 jObject?

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

尝试使用查询 Google 可用,但他们 return 包含 JSON 结果的附加 txt 文件。我是一名新手程序员,所以我无法弄清楚为什么我拍摄的任何照片都不起作用。

    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: 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\"]]\" |

下面是我从邮递员 google 得到的回复

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"
  ]
]

帮帮我,我已经搞了好几天了……在我弄清楚之前没有食物!!!!!

问题一 - 您使用了 reader.ReadToEnd() 两次。首先,当您尝试读取 errorMessage 时,然后在下一行再次使用它。到第二次时,您已经阅读了所有内容。 删除行:

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

问题二 - 看起来您接收的数据是一个数组,因此为了读取它您需要使用

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