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
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:
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):
Update:
You should be able to parse your initially posted Json like this:
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:
Usage: