WebApi HttpClient help getting collection (version .6)

1.4k views Asked by At

I am calling on a WCF Data Services v3 Odata service. I am having trouble getting my collection filled in the below example. I am able to get a json string of the 3 people, but if I try and get a custom collection filled, the collection has a count = 0.

HttpClient client = new HttpClient();

client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

client.BaseAddress = new Uri("http://localhost:7500/Wcf1.svc/People");

HttpResponseMessage resp = client.GetAsync("").Result;

string jsonString = resp.Content.ReadAsStringAsync().Result;

List<Person> personCollection = resp.Content.ReadAsAsync<List<Person>>().Result;

jsonString has 3 people in it.

personCollection has a count = 0.

the jsonString looks like this:

{"d":[
{"__metadata":{"id":"http://localhost:7500/Wcf1.svc/People(1)",
"uri":"http://localhost:7500/Wcf1.svc/People(1)",
"type":"WcfService1.Person"},
"ID":1,"Fname":"Fred","Lname":"Peters","Address1":"123 Main"},

 {"__metadata":{"id":"http://localhost:7500/Wcf1.svc/People(2)",
"uri":"http://localhost:7500/Wcf1.svc/People(2)",
"type":"WcfService1.Person"},
"ID":2,"Fname":"John","Lname":"Smith","Address1":"123 Oak"},

{"__metadata":{"id":"http://localhost:7500/Wcf1.svc/People(3)",
"uri":"http://localhost:7500/Wcf1.svc/People(3)",
"type":"WcfService1.Person"},
"ID":3,"Fname":"Tom","Lname":"Anders","Address1":"123 Hill St."}]}

I must be doing something wrong, please point out my error if you can.

Thanks. Terrence

1

There are 1 answers

13
Alexander Zeitler On BEST ANSWER

Your content is not a List<Person>

Paste your Json into json2csharp and you'll see it.

To get a better overview what your response content is, download Json Viewer - this is a screenshot of your data:

enter image description here

As you can see: the Persons are a property of the Json root object.

If you wanted to use your code from above, the Json should have to look like this (or you need to access the data in the given structure mapping you classes according to the Json):

[{"__metadata":{"id":"http://localhost:7500/Wcf1.svc/People(1)",
"uri":"http://localhost:7500/Wcf1.svc/People(1)",
"type":"WcfService1.Person"},
"ID":1,"Fname":"Fred","Lname":"Peters","Address1":"123 Main"},

 {"__metadata":{"id":"http://localhost:7500/Wcf1.svc/People(2)",
"uri":"http://localhost:7500/Wcf1.svc/People(2)",
"type":"WcfService1.Person"},
"ID":2,"Fname":"John","Lname":"Smith","Address1":"123 Oak"},

{"__metadata":{"id":"http://localhost:7500/Wcf1.svc/People(3)",
"uri":"http://localhost:7500/Wcf1.svc/People(3)",
"type":"WcfService1.Person"},
"ID":3,"Fname":"Tom","Lname":"Anders","Address1":"123 Hill St."}]}]

Update:

You should be able to parse your initially posted Json like this:

var json = JsonValue.Parse(response.Content.ReadAsStringAsync().Result);
var arr = json["d"];
var contact1 = arr[0];
var fname = result1["Fname"];

I have done a blog post on JsonValue and JsonArray recently. It's server side, but it should point you the direction.

2nd Update:

Using the classes from the json2csharp.com output, you can do this:

public class Metadata
{
    public string id { get; set; }
    public string uri { get; set; }
    public string type { get; set; }
}

public class D
{
    public Metadata __metadata { get; set; }
    public int ID { get; set; }
    public string Fname { get; set; }
    public string Lname { get; set; }
    public string Address1 { get; set; }
}

public class RootObject
{
    public List<D> d { get; set; }
}

Usage:

var root = resp.Content.ReadAsAsync<RootObject>().Result;
var persons = root.d;
var person1 = persons[0];