[英]Parsing a JSON Date/Time value using the Windows.Data.Json library in C#/WinRT project
[英]Parsing JSON from C# in WinRT
我正在開發一個適用於Windows 8的應用程序。我正在嘗試通過JSON對Twitter進行搜索。 為了實現這一目標,我使用以下博客文章作為參考。 http://blogs.microsoft.co.il/blogs/bursteg/archive/2008/11/26/twitter-api-from-c-searching.aspx
我的問題是,ASCIIEncoding類似乎不存在於WinRT框架中:(。我看到UTF8可用,但是,我不確定如何直接使用UTF8類。有人可以告訴我如何?
謝謝,
為了反序列化.NET中的JSON(完整的.NET和WinRT),我總是推薦使用JSON.NET 。 它比DataContractJsonSerializer或任何其他開箱即用的解決方案容易得多。 正如您在下面的代碼中看到的那樣,您不需要像在提供的示例中那樣定義編碼。
您只需要一個對象模型(使用json2csharp生成它)和幾行代碼:
HttpResponseMessage response = await HttpClient.GetAsync(someUri);
if (response.StatusCode == HttpStatusCode.OK)
{
string responseString = await response.Content.ReadAsStringAsync();
// parse to json
resultItem = JsonConvert.DeserializeObject<T>(responseString);
}
您可以嘗試使用Windows.Data.Json命名空間進行反序列化( http://msdn.microsoft.com/en-us/library/windows/apps/windows.data.json(v=VS.85).aspx )。 要獲得你的json,你可以使用這樣的東西:
HttpResponseMessage response = await client.GetAsync(url);
string responseText = await response.Content.ReadAsStringAsync();
只需用Encoding.UTF8
替換ASCIIEncoding.UTF8
- 它們本質上是同一個對象(靜態UTF8
屬性在桌面框架上的基本Encoding
類中定義)。 這在W8 metro應用程序中可用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.