C# 将httpwebresponse中的json响应解析为listView数据绑定窗口
我正在做一个http帖子,如下所示C# 将httpwebresponse中的json响应解析为listView数据绑定窗口,c#,json,listview,httpwebrequest,windows-phone-8.1,C#,Json,Listview,Httpwebrequest,Windows Phone 8.1,我正在做一个http帖子,如下所示 public async Task<string> HttppostJson() { // Create a request using a URL that can receive a post. HttpWebRequest request = HttpWebRequest.Create("URL") as HttpWebRequest; // Set the Method prope
public async Task<string> HttppostJson()
{
// Create a request using a URL that can receive a post.
HttpWebRequest request = HttpWebRequest.Create("URL") as HttpWebRequest;
// Set the Method property of the request to POST.
request.Method = "POST";
string post = "postData";
byte[] byteArray = Encoding.UTF8.GetBytes(post.ToString());
// Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded";
//Write data into a stream
using (var stream = await Task.Factory.FromAsync<Stream>(request.BeginGetRequestStream, request.EndGetRequestStream, null))
{
stream.Write(byteArray, 0, byteArray.Length);
}
// Pick up the response:
using (var response = (HttpWebResponse)(await Task<WebResponse>.Factory.FromAsync(request.BeginGetResponse, request.EndGetResponse, null)))
{
StreamReader reader = new StreamReader(response.GetResponseStream());
result = reader.ReadToEnd();
}
return result;
}
{
"id":20,
"title":"Testing after phase 2 is shifted to server",
"from":"Administrator",
"sendername":"ABX",
"day":"Tuesday",
"dateofMonth":"20th",
"month":"May",
"year":2014,
"date":"Tuesday, 20th May, 2014",
"content":"Ready. Set. Go"
}
还有19个这样的块引用每个id。我想反序列化对象并在列表视图中填充它们,在列表视图中应该只看到标题和所有20个通知中的内容。稍后将添加更多id,因此我希望使listView动态,其中应包括新添加的id以及相应的标题和来源。所以listview应该是这样的
Title= "title"_corresponding_to_id
subtitle= "from"_correspondin_to_id
Title= "title"_corresponding_to_id
subtitle= "from"_correspondin_to_id
Title= "title"_corresponding_to_id
subtitle= "from"_correspondin_to_id
..
..
..
我是Windows应用程序开发新手。所以需要一些帮助。你可以上电影课。
然后可以使用newtonsoft的Json.NET库和JObject类
您只需将json响应保存到字符串中,然后从JObject获取值并将其添加到列表中
string json = someJsonText
JObject response = JObject.Parse(json);
List<Movie> list = new List<Movie>();
Movie first = new Movie();
first.Title = response.GetValue("title");
list.Add(first);
在foreach或for sense中这样做,以获取、创建和保存所有电影。我是这样做的:var resp=wait HttppostJson;动态obj=JsonConvert.DeserializeObjectresp;obj{List items=new List;items.Addnew通知{title=o[title],from=o[from]};}中的foreach var o,但它只显示与id=0对应的标题和from。有什么想法吗?