![](/img/trans.png)
[英]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.