在 MonoTouch 中找不到 System.Runtime.Serialization.Json

发布于 2024-10-16 06:46:53 字数 313 浏览 1 评论 0原文

我是 Monotouch 的初学者。
我想使用 DataContractJsonSerializer 反序列化 Json 数据。但我无法在MonoDevelop中引用System.Runtime.Serialization.Json(仅System.Runtime.Serialization下的.Formatters)。我引用了System.Runtime.Serialization。我的配置和安装顺序是: 1.iPhone SDK 4.2 2. Mono 2.8.2(非CSDK版本) 3. Monotouch 3.2.4 评估 4. MonoDevelop 2.4

出现什么问题?

I am a beginner in Monotouch.
I would like to deserialize Json data using DataContractJsonSerializer. But I cannot reference System.Runtime.Serialization.Json(Only .Formatters under System.Runtime.Serialization) in MonoDevelop. I have referenced System.Runtime.Serialization. My config and installation sequences are:
1. iPhone SDK 4.2
2. Mono 2.8.2 (not CSDK version)
3. Monotouch 3.2.4 Eval
4. MonoDevelop 2.4

What is the problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

红ご颜醉 2024-10-23 06:46:53

MonoTouch 并没有附带 DataContractJSonSerializer,就像这个序列化器看起来那么简单,它引入了大量的库。

您可以使用 System.Json API,也可以尝试 NewtonSoft 的 JSon 库。

MonoTouch does not ship with a DataContractJSonSerializer as simple as this serializer looks, it brings in a large set of libraries.

You can use either the System.Json API or you can try NewtonSoft's JSon library.

海夕 2024-10-23 06:46:53

如果您像我一样尝试在跨平台代码库中使用 DataContractJsonSerializer,那么将 JSON.NET API(又名 Newtonsoft.Json)包装在 DataContractJsonSerializer 中就很容易了:

using System;
using System.IO;
using System.Reflection;
using Newtonsoft.Json;

namespace System.Runtime.Serialization.Json
{
public class DataContractJsonSerializer
{
    private Type type;
    private JsonSerializer js;

    public DataContractJsonSerializer (Type t)
    {
        this.type = t;
        this.js = new JsonSerializer();
    }

    public object ReadObject(Stream stream)
    {
        StreamReader reader = new StreamReader(stream);     
        return js.Deserialize(reader, type);
    }

    public void WriteObject(Stream stream, object o)
    {
        StreamWriter writer = new StreamWriter(stream);
        js.Serialize(writer, o);    
        writer.Flush ();
    }
}
}

当然,这就引出了一个问题:为什么不呢?到处都改用 JSON.NET API...我个人对该 API 的体验是,它可能比使用 DCJS 慢(至少在我对 Windows Phone 的非正式测试中)。

希望有帮助!

If you're like me and are trying to use DataContractJsonSerializer in a cross-platform codebase, it is easy enough to wrap the JSON.NET API (aka Newtonsoft.Json) in a DataContractJsonSerializer:

using System;
using System.IO;
using System.Reflection;
using Newtonsoft.Json;

namespace System.Runtime.Serialization.Json
{
public class DataContractJsonSerializer
{
    private Type type;
    private JsonSerializer js;

    public DataContractJsonSerializer (Type t)
    {
        this.type = t;
        this.js = new JsonSerializer();
    }

    public object ReadObject(Stream stream)
    {
        StreamReader reader = new StreamReader(stream);     
        return js.Deserialize(reader, type);
    }

    public void WriteObject(Stream stream, object o)
    {
        StreamWriter writer = new StreamWriter(stream);
        js.Serialize(writer, o);    
        writer.Flush ();
    }
}
}

Of course, that begs the question of why not switch to using the JSON.NET API everywhere... my personal experience with that API is that it can be slower than using DCJS (at least in my informal tests on Windows Phone).

Hope that helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文