我只是创建简单的JsonDataManager以便以JSON格式编写并解析它以便稍后使用它,但JObject.Parse
不等到webclient获取JSON数据并直接使用"它将会是由JSON填充"串。我怎么能推它等到它完全得到JSON字符串?
public class JsonDataManager{
String myData = "its going to be filled by json";
public JsonDataManager(string Category)
{
String url = "<a href="http://www.kaanbarisbayrak.com/?json=get_category_posts&cat="+Category;<br">http://www.kaanbarisbayrak.com/?json=get_category_posts&cat="+Catego...</a> />
WebClient wc = new WebClient();
wc.Encoding = System.Text.Encoding.UTF8;
wc.Headers["Accept"] = "application/json";
wc.DownloadStringAsync(new Uri(url), UriKind.Relative);
wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
}
public String getWriting()
{
JObject obj = JObject.Parse(myData);
JArray array = (JArray)obj["posts"];
string writing = (string)array[0]["content"];
return writing;
}
private void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
myData = e.Result;
}
}
答案 0 :(得分:1)
您需要等到WebClient完成下载。 一种方法是使用ManualResetEvent:
public class JsonDataManager
{
String myData = "its going to be filled by json";
ManualResetEvent mre = new ManualResetEvent(false);
public JsonDataManager(string Category)
{
String url = "<a href="http://www.kaanbarisbayrak.com/?json=get_category_posts&cat="+Category;<br">http://www.kaanbarisbayrak.com/?json=get_category_posts&cat="+Catego...</a> />
WebClient wc = new WebClient();
wc.Encoding = System.Text.Encoding.UTF8;
wc.Headers["Accept"] = "application/json";
wc.DownloadStringAsync(new Uri(url), UriKind.Relative);
wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
}
public String getWriting()
{
mre.WaitOne();
JObject obj = JObject.Parse(myData);
JArray array = (JArray)obj["posts"];
string writing = (string)array[0]["content"];
return writing;
}
private void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
myData = e.Result;
mre.Set();
}
}
了解有关重置事件的更多here。
答案 1 :(得分:0)
public class JsonDataManager
{
readonly String _url;
public JsonDataManager(string category)
{
_url = @"http://www.kaanbarisbayrak.com/?json=get_category_posts&cat="+category;
}
public async Task<String> getWriting()
{
WebClient wc = new WebClient();
wc.Encoding = System.Text.Encoding.UTF8;
wc.Headers["Accept"] = "application/json";
JObject obj = JObject.Parse(await wc.DownloadStringTaskAsnync(_url));
JArray array = (JArray)obj["posts"];
string writing = (string)array[0]["content"];
return writing;
}
}
使用它:
var jsonManager = new JsonDataManager("whatever");
var writing = await jsonManager.getWriting();
此代码未经过测试,但我希望您明白这一点。
答案 2 :(得分:0)
如果您尝试发布数据并获得json结果,我建议您使用 StreamReader 来正确获取响应。请看下面的方法,它会将结果作为字符串返回,你可以将它解析为json。
public string GetJsonResult(string url, string queryStringValues) { // Create web request HttpWebRequest httpWebRequest; httpWebRequest = (HttpWebRequest)WebRequest.Create(url + queryStringValues); httpWebRequest.Method = "POST"; httpWebRequest.Timeout = 6000; httpWebRequest.ContentType = "application/json"; System.Net.ServicePointManager.Expect100Continue = false; // Make the request and get the response HttpWebResponse response = null; response = (HttpWebResponse)httpWebRequest.GetResponse(); string content = ""; using (var stream = new StreamReader(response.GetResponseStream())) { content = stream.ReadToEnd(); } return content; }
答案 3 :(得分:0)
我想你想使用Task<string>
。这意味着getWriting()
是最终返回字符串但不会阻塞该线程的函数。要执行此操作,只需将下载调用包装在任务中即可。然后,您可以附加一个ContinueWith
函数作为您的回调函数,该函数接收结果并将其打印到屏幕上。
class JsonDataManager
{
String url = "http://www.kaanbarisbayrak.com/?json=get_category_posts&cat=";
string data = "";
HttpClient hc;
public JsonDataManager(string Category)
{
hc = new HttpClient();
url += Category;
}
public Task<string> getWriting()
{
return Task.Factory.StartNew<string>(() =>
{
data = hc.GetStringAsync(url).Result;
// use the resulting string
JObject obj = JObject.Parse(data);
JArray array = (JArray)obj["posts"];
string writing = (string)array[0]["content"];
return writing;
});
}
}
private void Form1_Load(object sender, EventArgs e)
{
JsonDataManager manager = new JsonDataManager("4");
manager.getWriting().ContinueWith((task) =>
{
string writing = task.Result;
MessageBox.Show(writing);
});
}