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

本文关键字:请求 web 转换 jObject txt 包含 json | 更新日期: 2023-09-27 18:13:57

尝试使用Google提供的查询,但它们返回一个包含JSON结果的附加文本文件。我是一个新手程序员,所以我不明白为什么我拍的照片都不起作用。

    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 read jobobject fromJsonReader。当前JsonReader项不是一个对象:字符串。路径",1号线,2号位置。' r ' nNewtonsoft.Json.Linq.JObject。加载JsonReader阅读器,JsonLoadSettings'r'n at Newtonsoft.Json.Linq.JObject。Parse (String json,JsonLoadSettings设置)'r'n atOdsCode.Services.YouTubeSearchService.d__3.MoveNext()",

"服务器获取搜索结果错误:'['"搜索'",'"搜索'",'"搜索并摧毁'",'"寻找我宝贝鲍比摩尔'",'"搜索和丢弃'",'"搜索的"最差","搜索youtube","搜索引擎"优化"、"搜索"、"搜索历史"、"搜索方萨姆"布鲁诺 '"]]'" |

下面是我从google在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"
  ]
]

帮帮我,我已经忙了好几天了…在我弄清楚之前不吃东西!!!!!

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

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

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

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

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