Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在MVVm cross Xamarin中解析对模型类的网络响应_C#_Xamarin_Xamarin.android_Mvvmcross - Fatal编程技术网

C# 如何在MVVm cross Xamarin中解析对模型类的网络响应

C# 如何在MVVm cross Xamarin中解析对模型类的网络响应,c#,xamarin,xamarin.android,mvvmcross,C#,Xamarin,Xamarin.android,Mvvmcross,我拥有的:我拥有一个从API获取响应的服务 我正在尝试做的事情: 我试图将数据解析为一个模型对象(这里我有一个 (人民) 过去是安卓程序员,收到回复后 从服务器上,我通常使用GSON用数据填充模型对象 如何在MvvmCross xamarin中实现同样的功能 RestService.cs public class RestService : IntIRestService { public async Task<List<People>>

我拥有的:我拥有一个从API获取响应的服务


我正在尝试做的事情

  • 我试图将数据解析为一个模型对象(这里我有一个 (人民)
  • 过去是安卓程序员,收到回复后 从服务器上,我通常使用GSON用数据填充模型对象
  • 如何在MvvmCross xamarin中实现同样的功能

RestService.cs

public class RestService : IntIRestService
    {
        public async Task<List<People>> GetSalesPeopleAsync()
        {
            try
            {   
                //Declare a Http client
                var client = new HttpClient();
                //Add a Base URl
                client.BaseAddress = new Uri(Constants.MUrl);
                //Add the response type
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                //Add the API
                Task<string> response = client.GetStringAsync("iCodersLab/Custom-ListView-Using-Volley/master/richman.json");

                String  content = await response;

                //var list = JsonConvert.DeserializeObject<List<People>>(content).ToList();

                //return content;
            }
            catch (Exception ex)
            {
                var t = ex.Message;
            }

            return null;
        }
    }
namespace SqliteDemo.core.Models
{
    public class People
    {
        public String name
        {
            get;
            set;
        }

        public String image
        {
            get;
            set;
        }

        public String worth
        {
            get;
            set;
        }

        public int InYear
        {
            get;
            set;
        }

        public String source
        {
            get;
            set;
        }
    }
}

输出::

当前在
GetSalesoperEasync()
中给出了以下输出


使用Newtonsoft.JsonConvert.DeserializeObject(内容)

什么是
Newtonsoft
它是一个库吗?我们需要为这个核心项目添加任何
Nuget
包吗?谢谢,我需要传递什么到
content
,我可以传递我收到的输出吗?你可以添加一些代码并返回类型吗?它与你在代码中显示的对象相同:JSON字符串。返回类型是人或者任何你想在输出中得到的类型。只需在
反序列化对象(json)方法中提供类型参数
。它与AndroidAnup中的gson相同,如果有任何教程示例链接,请在这里发布文档。
[\n{\n        \"name\": \"First Richman\",\n        \"image\": \"https://raw.githubusercontent.com/iCodersLab/Custom-ListView-Using-Volley/master/images/Bill_Gates.jpg\",\n        \"worth\": \"$89 billion\",\n        \"InYear\": 2015,\n        \"source\": \"Microsoft\"\n    },\n    {\n        \"name\": \"Second Richman\",\n        \"image\": \"https://raw.githubusercontent.com/iCodersLab/Custom-ListView-Using-Volley/master/images/Carlos_Slim_Helu.jpg\",\n        \"worth\": \"$85 billion\",\n        \"InYear\": 2015,\n        \"source\": \"Telmex, Grupo Carso\"\n    },\n    {\n        \"name\": \"Third Richman\",\n                \"image\": \"https://raw.githubusercontent.com/iCodersLab/Custom-ListView-Using-Volley/master/images/WarrenBuffett.jpg\",\n                \"worth\": \"$77 billion\",\n                \"InYear\": 2015,\n                \"source\": \"Berkshire Hathaway\"\n    },\n    {\n        \"name\": \"Fourth Richman\",\n                \"image\": \"https://raw.githubusercontent.com/iCodersLab/Custom-ListView-Using-Volley/master/images/amancio_ortega.jpg\",\n                \"worth\": \"$70 billion\",\n                \"InYear\": 2015,\n                \"source\": \"Inditex Group\"\n    },\n    {\n       \"name\": \"Fifth Richman\",\n               \"image\": \"https://raw.githubusercontent.com/iCodersLab/Custom-ListView-Using-Volley/master/images/Larry_Elllison.jpg\",\n               \"worth\": \"$65 billion\",\n               \"InYear\": 2015,\n               \"source\": \"Oracle Corporation\"\n    },\n    {\n        \"name\": \"Sixth Richman\",\n                \"image\": \"https://raw.githubusercontent.com/iCodersLab/Custom-ListView-Using-Volley/master/images/cgkochwide.jpg\",\n                \"worth\": \"$45 billion\",\n                \"InYear\": 2015,\n                \"source\": \"Koch Industries\"\n    },\n    {\n        \"name\": \"Seventh Richman\",\n                \"image\": \"https://raw.githubusercontent.com/iCodersLab/Custom-ListView-Using-Volley/master/images/David_Kochcrop2007.jpg\",\n                \"worth\": \"$42 billion\",\n                \"InYear\": 2015,\n                \"source\": \"Koch Industries\"\n    },\n    {\n        \"name\": \"Christy Walton\",\n                \"image\": \"https://raw.githubusercontent.com/iCodersLab/Custom-ListView-Using-Volley/master/images/christywalton.jpg\",\n                \"worth\": \"$41.7 billion \",\n                \"InYear\": 2015,\n                \"source\": \"Wal-Mart\"\n    },\n    {\n        \"name\": \"Jim Walton\",\n                \"image\": \"https://raw.githubusercontent.com/iCodersLab/Custom-ListView-Using-Volley/master/images/Jim_Walton.jpg\",\n                \"worth\": \"$40.6 billion\",\n                \"InYear\": 2015,\n                \"source\": \"Wal-Mart\"\n    },\n    {\n       \"name\": \"Liliane Bettencourt\",\n               \"image\": \"https://raw.githubusercontent.com/iCodersLab/Custom-ListView-Using-Volley/master/images/lilianebettencourt.jpg\",\n               \"worth\": \"$40.1 billion\",\n               \"InYear\": 2015,\n               \"source\": \"L'Oreal\"\n    },\n    {\n        \"name\": \"Carlos Slim & family\",\n                \"image\": \"https://raw.githubusercontent.com/iCodersLab/Custom-ListView-Using-Volley/master/images/CarlosSlimfamily.jpg\",\n                \"worth\": \"$79.2 billion\",\n                \"InYear\": 2014,\n                \"source\": \"Telmex, Am�rica M?vil, Grupo Carso\"\n    },\n    {\n       \"name\": \"Bernard Arnault\",\n               \"image\": \"https://raw.githubusercontent.com/iCodersLab/Custom-ListView-Using-Volley/master/images/Bernard_Arnault.jpg\",\n               \"worth\": \"$29.0 billion\",\n               \"InYear\": 2013,\n               \"source\": \"LVMH Mo�t Hennessy � Louis Vuitton\"\n    },\n    {\n        \"name\": \"Prince Al-Waleed\",\n                \"image\": \"https://raw.githubusercontent.com/iCodersLab/Custom-ListView-Using-Volley/master/images/PrinceAlwaleed.jpg\",\n                \"worth\": \"$23.7 billion\",\n                \"InYear\": 2005,\n                \"source\": \"Kingdom Holding Company\"\n    }\n    ]