办法从HttpWebResponse反序列化JSON没有第三方框架 [英] Way to Deserialize JSON from HttpWebResponse without 3rd party frameworks

查看:138
本文介绍了办法从HttpWebResponse反序列化JSON没有第三方框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从依赖于开源或第三方库,如Json.NET从一个HttpWebResponse解析传入的JSON保持。为什么?由于开源框架更加依赖,在你的帮助实现,更多的你的应用程序必须依靠这些依赖......我不喜欢我的应用程序要在大量的库depenent的原因有很多,如果在所有可能的。我确定有因为它是由MS支持,但我花更多的开源库使用的东西,如企业库。

I'm trying to keep from depending on open source or third party libraries such as Json.NET to parse incoming JSON from an HttpWebResponse. Why? Because the more reliance on open source frameworks to aid in your implementations, the more your app has to rely on those dependencies...I don't like my apps to be depenent on a lot of libraries for many reasons if at all possible. I'm ok with using stuff like Enterprise Library because it's supported by MS but I'm taking more open source libraries.

无论如何,我想弄清楚在.NET 3.5来解析传入的JSON服务器端的最佳方式。

Anyway, I'm trying to figure out the best way to parse incoming JSON server-side in .NET 3.5.

我知道这是会得到很多响应,我甚至使用了.NET 3.5的JavaScriptSerializer串行化数据为JSON但我现在想弄清楚做反向没有最好的,最简单的方法再次,不必使用第三方/开源框架,在此提供帮助。

I know this is going to get a lot of responses and I've even used the .NET 3.5 JavaScriptSerializer to serialize data to JSON but now I'm trying to figure out the best and most simple way to do the reverse without again, having to use a 3rd party / open source framework to aid in this.

推荐答案

微软建议的 JSON序列化是<一个href=\"http://msdn.microsoft.com/en-us/library/bb410770.aspx\"><$c$c>DataContractJsonSerializer这个类中存在的 System.Runtime.Serialization 组装

该示例演示从JSON数据反序列化到一个对象。

The sample demonstrates deserializing from JSON data into an object.

MemoryStream stream1 = new MemoryStream();     
Person p2 = (Person)ser.ReadObject(stream1);

要序列化Person类型为JSON的一个实例,首先创建DataContractJsonSerializer并使用writeObject方法以JSON数据写入流。

To serialize an instance of the Person type to JSON, create the DataContractJsonSerializer first and use the WriteObject method to write JSON data to a stream.

Person p = new Person();
//Set up Person object...
MemoryStream stream1 = new MemoryStream();
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Person));
ser.WriteObject(stream1, p);

更新:添加辅助类

下面是您可以使用一个样本辅助类简单/从JSON序列:

Here is a sample helper class that you can use for simple To/From Json serialization:

public static class JsonHelper
{
    public static string ToJson<T>(T instance)
    {
        var serializer = new DataContractJsonSerializer(typeof(T));
        using (var tempStream = new MemoryStream())
        {
            serializer.WriteObject(tempStream, instance);
            return Encoding.Default.GetString(tempStream.ToArray());
        }
    }

    public static T FromJson<T>(string json)
    {
        var serializer = new DataContractJsonSerializer(typeof(T));
        using (var tempStream = new MemoryStream(Encoding.Unicode.GetBytes(json)))
        {
            return (T)serializer.ReadObject(tempStream);
        }
    }
}

这篇关于办法从HttpWebResponse反序列化JSON没有第三方框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆