简体   繁体   English

用Json数据填充列表并从中获取对象

[英]Filling list with Json data and getting the objects from it

I have a php webservice which gets the data from database and returns it as json data. 我有一个php webservice,它从数据库获取数据并将其作为json数据返回。

Json data 杰森数据

{"faqs":
   [
        {"faq":{"id":"123"}},
        {"faq":{"id":"124"}}
   ]
}

Object classes 对象类

public class FaqList
{
    public List<Faq> faqs { get; set; }
}
public class Faq
{
    public string id { get; set; }
}

Test class 测试班

var client = new HttpClient();
HttpResponseMessage response = await client.GetAsync(new Uri("http://www.mydomain.com/webservice/7/server.php"));
var jsonString = await response.Content.ReadAsStringAsync();

FaqList list = JsonConvert.DeserializeObject<FaqList>(jsonString);

list.faqs.Count() => 2
list.faqs[0].id   => NULL !!

I fill all the objects to the 'list'. 我将所有对象填充到“列表”中。 With count test I see that it's filled. 通过计数测试,我看到它已满。 But if I try to get an objects from it, I get null error. 但是,如果我尝试从中获取对象,则会得到null错误。

So, how can I correctly fill my list so that I can get the objects from it? 那么,如何正确填写我的列表,以便可以从中获取对象?

http://json2csharp.com/ suggests these definitions. http://json2csharp.com/提出了这些定义。 (One more class Faq2 ) (再上一堂Faq2

public class Faq2
{
    public string id { get; set; }
}

public class Faq
{
    public Faq2 faq { get; set; }
}

public class RootObject
{
    public List<Faq> faqs { get; set; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM