我得到了以下代码,用于从我的web服务中检索数据:
using System;
using System.Collections.Generic;
using System.Windows;
using Microsoft.Phone.Controls;
using Newtonsoft.Json.Linq;
using RestSharp;
namespace BlogIT
{
public partial class BlogPage : PhoneApplicationPage
{
public BlogPage()
{
InitializeComponent();
// listbox vullen met alle posts
var client = new RestClient();
client.BaseUrl = "http://localhost:8080/blog/api";
RestRequest request = new RestRequest();
request.Resource = "posts";
request.RequestFormat = DataFormat.Json;
request.Method = Method.GET;
client.ExecuteAsync<List<CreatePost>>(request, (response)
=>
{
if (response.ResponseStatus != ResponseStatus.Error
&& response.StatusCode == System.Net.HttpStatusCode.OK)
{
var x = response.Content;
}
else
{
MessageBox.Show("Error occured.");
}
});
}
}
}
Var x包含以下Json字符串:
[
{"blog":
{"blogNaam":"Kevinsblog",
"owner":
{"username":"Kevin"}
},
"postbeschrijving":"Dit is een post om te testen",
"postId":1,
"titel":"TestPost"
},
{"blog":
{"blogNaam":"Kevinsblog",
"owner":
{"username":"Kevin"}
},
"postbeschrijving":"Dit is een post om te testen2",
"postId":2,
"titel":"TestPost2"
},
{"blog":
{"blogNaam":"Kevinsblog",
"owner":
{"username":"Kevin"}
},
"postbeschrijving":"Dit is een post om te testen3",
"postId":3,
"titel":"TestPost3"
}
]
第一个问题是如何通过Json字符串循环并将其放入数组中所以数组[0]包含
{"blog":
{"blogNaam":"Kevinsblog",
"owner":
{"username":"Kevin"}
},
"postbeschrijving":"Dit is een post om te testen",
"postId":1,
"titel":"TestPost"
}
数组[1]包含
{"blog":
{"blogNaam":"Kevinsblog",
"owner":
{"username":"Kevin"}
},
"postbeschrijving":"Dit is een post om te testen2",
"postId":2,
"titel":"TestPost2"
}
数组[2]包含
{"blog":
{"blogNaam":"Kevinsblog",
"owner":
{"username":"Kevin"}
},
"postbeschrijving":"Dit is een post om te testen3",
"postId":3,
"titel":"TestPost3"
}
第二个问题是,一旦这些值被放入数组,如何访问它们,这样我就可以将它们放入我的WP7应用中的列表框中
如果需要任何其他代码,只需询问即可。
它非常简单。首先创建一些类来匹配JSON:
public class Post
{
public Blog blog { get; set; }
public string postbeschrijving { get; set; }
public int postId { get; set; }
public string titel { get; set; }
}
public class Blog
{
public string blogNaam { get; set; }
public Owner owner { get; set; }
}
public class Owner
{
public string username { get; set; }
}
然后,您可以反序列化为以下数组:
Post[] posts = JsonConvert.DeserializeObject<Post[]>(x);
现在,您的数据包含在阵列中,您可以像往常一样访问它,例如:
for (int i = 0; i < posts.Length; i++)
{
Console.WriteLine("post " + i);
Console.WriteLine("blogNaam:" + posts[i].blog.blogNaam);
Console.WriteLine("username:" + posts[i].blog.owner.username);
Console.WriteLine("postbeschrijving:" + posts[i].postbeschrijving);
Console.WriteLine("postId:" + posts[i].postId);
Console.WriteLine("titel:" + posts[i].titel);
Console.WriteLine("-----");
}
假设x
包含您问题中的JSON,如果在控制台应用程序中运行,上面的内容将给出以下输出:
post 0
blogNaam:Kevinsblog
username:Kevin
postbeschrijving:Dit is een post om te testen
postId:1
titel:TestPost
-----
post 1
blogNaam:Kevinsblog
username:Kevin
postbeschrijving:Dit is een post om te testen2
postId:2
titel:TestPost2
-----
post 2
blogNaam:Kevinsblog
username:Kevin
postbeschrijving:Dit is een post om te testen3
postId:3
titel:TestPost3
-----