我有一个外部API,它返回如下响应:
{
"meta": {
"year": "...",
"month": "...",
"reasons": "...",
"api_data": "..."
},
"results": [
{
"name": "David",
"age": 43
},
{
"name": "Jason",
"age": 23
},
{
"name": "Nissan",
"age": 32
},
...
}
I want to parse only the results array , no need for the meta
proprety.
我创建了一个Employee类:
public class Employee
{
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("age")]
public int Age { get; set; }
}
并向API发送请求:
public HttpResponseMessage Get(String reaction)
{
String apiUrl = "............";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(apiUrl);
WebResponse response = request.GetResponse();
using (Stream responseStream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
List<Employee> myObjectList = null;
var results = reader.ReadToEnd();
if (results != null)
{
dynamic data = JObject.Parse(results);
// manipulate data
// The following doesn't work since I've already parsed the data
// myObjectList = JsonConvert.DeserializeObject<List<Employee>>(data.results);
return Request.CreateResponse(HttpStatusCode.OK, myObjectList);
}
}
return Request.CreateResponse(HttpStatusCode.OK, "");
}
How can I grab all the results
array straight into a list of Employee ?