response.content.readasstringasync() 到对象 [英] response.content.readasstringasync() to object

查看:248
本文介绍了response.content.readasstringasync() 到对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们!

我的问题是我无法提取 api 发送的对象,尽管我在 API 或客户端中有对象的架构.我的代码

My problem is that i can not extract an object sent by an api, despite i have the schema of the object either in the API or in the client. my code

 public async Task<ActionResult> Index()
    {
        HttpClient client = new HttpClient();


        Uri baseAddress = new Uri("http://localhost:44237/");
        client.BaseAddress = baseAddress;


        HttpResponseMessage response = client.GetAsync("api/Front/Subm?IdSubmission=1xxx").Result;

        try
        {
            if (response.IsSuccessStatusCode)
            {


                string Result = await response.Content.ReadAsStringAsync();
                JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
                Submission sub = JsonConvert.DeserializeObject<Submission>(Result);

                return View(sub);
            }
            else
            {

            }
        }
        catch (Exception e)
        {

        }
    }

结果中收到的结构是在此处输入图片描述

但我的对象总是:在此处输入图片描述

感谢您的帮助!!!!

推荐答案

您没有使用在您的代码中创建的实例 JavaScriptSerializer:

You didn't use the instance JavaScriptSerializer which is created in your code:

JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
Submission sub = JsonConvert.DeserializeObject<Submission>(Result);

试着像这样修改你的代码:

Try to modify your code like this:

string Result = await response.Content.ReadAsStringAsync();
JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
Submission sub = jsonSerializer.DeserializeObject(Result);

这篇关于response.content.readasstringasync() 到对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆