Api rest Parse Json Array c#



我正在尝试通过 linguee API 解析 json,但它不起作用,我无法弄清楚为什么。我想获取 JsonArray "real_examples",但我仍然有一个错误。

这是错误:

未处理的异常:

Newtonsoft.Json.JsonReaderException:解析值时遇到意外字符:S. 路径 '',第 0 行,位置 0。

这是代码:

public  class MyLinguee
{
    private const string URL = "https://leafword.herokuapp.com/api";
    private string urlParameters = "?q=parler&src=fr&dst=en";
    public void MyDescript()
    {

    HttpClient client = new HttpClient();
        client.BaseAddress = new Uri(URL);
        // Add an Accept header for JSON format.
        client.DefaultRequestHeaders.Accept.Add(
        new MediaTypeWithQualityHeaderValue("application/json"));
        // List data response.
        HttpResponseMessage response = client.GetAsync(urlParameters).Result;  // Blocking call!
        if (response.IsSuccessStatusCode)
        {
           JObject joResponse = JObject.Parse(response.ToString());
            JArray MyDescriptList = (JArray)joResponse["real_examples"];
            string src = Convert.ToString(MyDescriptList[0]);
            Console.WriteLine("descrypt : " + src);
        }
        else
        {
            Console.WriteLine("descryptdo not work");
            Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
        }
    }
}

这是 Json 格式:

{
    "src_lang": "fr",
    "dst_lang": "en",
    "query": "parler",
    "correct_query": "parler",
    "inexact_matches": null,
    "real_examples": [
        {
            "id": "",
            "src": "Pour ne pas parler d'autres objectifs [...] consacrés auparavant et réaffirmés maintenant.",
            "dst": "Not to mention other objectives [...] which had been previously enshrined and which have now been reaffirmed.",
            "url": "http://www.europarl.europa.eu/sides/getDoc.do?pubRef=-//EP//TEXT+CRE+20020925+ITEMS+DOC+XML+V0//FRu0026amp;language=FR"
        },
        {
            "id": "",
            "src": "Cela prouve que l'on ne peut pas parler de handicap, que nous sommes considérés comme des participants à part entière [...] dans les discussions économiques.",
            "dst": "This shows that there is no obstacle and that we are seen as a fully fledged participant in economic discussions.",
            "url": "http://consilium.europa.eu/showFocus.aspx?lang=FRu0026focusID=340"
        },
        {
            "id": "",
            "src": "Ce succès s'explique par la qualité des préparatifs menés à tous les niveaux par l'ensemble des acteurs concernés, sans parler de l'accueil enthousiaste de la part du public.",
            "dst": "This success can be attributed to the quality of the preparations made by all players involved and at all levels, not to mention the enthusiastic reception of the general public.",
            "url": "http://www.ecb.europa.eu/press/key/date/2005/html/sp050114.fr.html"
        }
    ]
}

响应由其中的许多属性组成 内容,这是你感兴趣的,但它的类型是 StreamContent 和 ToString(( 不会给你所需的输出。幸运的是,StreamContent有一个读取字符串的方法。尝试将阅读响应更改为:

JObject joResponse = JObject.Parse(await response.Content.ReadAsStringAsync());

并在您的签名方法中添加异步关键字:

public async static void MyDescript()

最新更新